我想读取目录中的所有文件并附加它们。我写了代码1 ,但它没有正常工作。我只在目录中放置了两个文件(进行测试),并将代码1 更改为代码2 ,这些文件正常工作。我不明白为什么 Code 1 无效。
library(data.table)
#### Code 1
for (file in list.files()){
# if the merged dataset doesn't exist, create it
if (!exists("dataset")){
dataset <- fread(file, header=TRUE, sep=",")
}
# if the merged dataset does exist, append to it
if (exists("dataset")){
temp_dataset <-fread(file, header=TRUE, sep=",")
**dataset**<-rbind(dataset, temp_dataset)
rm(temp_dataset)
}
}
#### Code 2
for (file in list.files()){
# if the merged dataset doesn't exist, create it
if (!exists("dataset")){
dataset <- fread(file, header=TRUE, sep=",")
}
# if the merged dataset does exist, append to it
if (exists("dataset")){
temp_dataset <-fread(file, header=TRUE, sep=",")
**dataset2**<-rbind(dataset, temp_dataset)
rm(temp_dataset)
}
}