data.table::fwrite 在保存文件名时表现异常
data.table::fwrite behaving strange when saving file names
我有一个非常大的数据框,我正在对 lapply
中的列子集执行多项操作,其中包括使用 data.table::fwrite
导出子集
我有多个列,它们的字母相同但大小写不同(例如 Co2 和 CO2)。当我使用 dplyr::select
获得列名和 运行 lapply
中的过程时,正确的列是 selected(即正确的情况是 selected),但是当我导出数据时,一些导出的文件名大小写不正确,而另一些则大小写正确。
例如,下面是一个示例数据框
test_df <- data.frame('CO2' = c(1,2,3,4,5),
'Co2' = c(6,7,8,9,10))
CO2 Co2
1 1 6
2 2 7
3 3 8
4 4 9
5 5 10
下面是我用来 select lapply
中使用的列的代码,然后是使用 lapply
的简单数据集子集(注意输出是预期的) .
test_colnames <- test_df %>% colnames()
lapply(test_colnames, function(x){
var_name <- all_of(x)
export <- test_df %>%
select(!!var_name) })
[[1]]
CO2
1 1
2 2
3 3
4 4
5 5
[[2]]
Co2
1 6
2 7
3 8
4 9
5 10
当我尝试使用下面的代码导出子集时,我只得到一个名为 CO2 的输出文件(而不是两个),但包含来自 Co2 的数据。输出名称(但不是列名称)以某种方式将 Co2 大写并覆盖了 CO2 文件。
lapply(test_colnames, function(x){
var_name <- all_of(x)
export <- test_df %>%
select(!!var_name)
export %>% data.table::fwrite(paste0(data_directory, "test_out_", all_of(x), ".csv")) })
只输出了一个名为test_out_CO2.csv的文件,下面是文件的内容。
Co2
1 6
2 7
3 8
4 9
5 10
有什么解决办法吗?
编辑: 简化示例,下面将只输出一个文件 - "testName.txt":
library(data.table)
fwrite(mtcars[1:2], "testName.txt")
fwrite(mtcars[3:4], "testname.txt")
这不是 data.table 特定问题,Windows 文件名不区分大小写。
我建议使列名称非常独特:
colnames(test_df) <- make.unique(tolower(colnames(test_df)), sep = "_")
colnames(test_df)
# [1] "co2" "co2_1"
# ... rest of your code
感谢zx8754的解答
根据 zx8754 提供的信息,修改后的解决方案将保留变量的大小写,同时确保文件名对于 windows 是唯一的,这将是一个 if else 语句。
lapply(test_colnames, function(x){
var_name <- all_of(x)
export <- test_df %>%
select(!!var_name)
if (file.exists(paste0(data_directory, "test_out_", all_of(x), ".csv"))) {
export %>% data.table::fwrite(paste0(data_directory, "test_out_", all_of(x), "_1.csv")) }
else {
export %>% data.table::fwrite(paste0(data_directory, "test_out_", all_of(x), ".csv")) }
})
我有一个非常大的数据框,我正在对 lapply
中的列子集执行多项操作,其中包括使用 data.table::fwrite
我有多个列,它们的字母相同但大小写不同(例如 Co2 和 CO2)。当我使用 dplyr::select
获得列名和 运行 lapply
中的过程时,正确的列是 selected(即正确的情况是 selected),但是当我导出数据时,一些导出的文件名大小写不正确,而另一些则大小写正确。
例如,下面是一个示例数据框
test_df <- data.frame('CO2' = c(1,2,3,4,5),
'Co2' = c(6,7,8,9,10))
CO2 Co2
1 1 6
2 2 7
3 3 8
4 4 9
5 5 10
下面是我用来 select lapply
中使用的列的代码,然后是使用 lapply
的简单数据集子集(注意输出是预期的) .
test_colnames <- test_df %>% colnames()
lapply(test_colnames, function(x){
var_name <- all_of(x)
export <- test_df %>%
select(!!var_name) })
[[1]]
CO2
1 1
2 2
3 3
4 4
5 5
[[2]]
Co2
1 6
2 7
3 8
4 9
5 10
当我尝试使用下面的代码导出子集时,我只得到一个名为 CO2 的输出文件(而不是两个),但包含来自 Co2 的数据。输出名称(但不是列名称)以某种方式将 Co2 大写并覆盖了 CO2 文件。
lapply(test_colnames, function(x){
var_name <- all_of(x)
export <- test_df %>%
select(!!var_name)
export %>% data.table::fwrite(paste0(data_directory, "test_out_", all_of(x), ".csv")) })
只输出了一个名为test_out_CO2.csv的文件,下面是文件的内容。
Co2
1 6
2 7
3 8
4 9
5 10
有什么解决办法吗?
编辑: 简化示例,下面将只输出一个文件 - "testName.txt":
library(data.table)
fwrite(mtcars[1:2], "testName.txt")
fwrite(mtcars[3:4], "testname.txt")
这不是 data.table 特定问题,Windows 文件名不区分大小写。
我建议使列名称非常独特:
colnames(test_df) <- make.unique(tolower(colnames(test_df)), sep = "_")
colnames(test_df)
# [1] "co2" "co2_1"
# ... rest of your code
感谢zx8754的解答
根据 zx8754 提供的信息,修改后的解决方案将保留变量的大小写,同时确保文件名对于 windows 是唯一的,这将是一个 if else 语句。
lapply(test_colnames, function(x){
var_name <- all_of(x)
export <- test_df %>%
select(!!var_name)
if (file.exists(paste0(data_directory, "test_out_", all_of(x), ".csv"))) {
export %>% data.table::fwrite(paste0(data_directory, "test_out_", all_of(x), "_1.csv")) }
else {
export %>% data.table::fwrite(paste0(data_directory, "test_out_", all_of(x), ".csv")) }
})