我有大量按每个类别组织的.csv数据文件。我想将它们导入到每个类别的列表中。
为此,我已经为每个类别创建了一个路径文件列表:
path_list <- list(a = c("path1","path2"), b= c("path3", "path4","path5"))
然后,我希望将“.csv”文件读入与path_list
具有相同结构的新列表中:
data_list <- vector("list", length(path_list))
names(data_list) <- names(path_list)
for(i in 1:length(data_list)) {
for (j in 1:3)) {
data_list[[i]] <- read.csv(path_list[[i]][[j]], header=T, na.strings = "", stringsAsFactors = F)
print (i)
}
}
这适用于path_list$a
的前两个元素,但后来我收到了错误:
Error in path_list[[i]][[j]] : subscript out of bounds
我知道的是因为j
在实际j
循环1:3
时只有两个元素。有没有办法让j
为每个列表元素动态化?
最终结果应如下所示:
data_list
$a
[1] "datafile1" "datafile2"
$b
[1] "datafile3" "datafile4" "datafile5"
答案 0 :(得分:1)
它是越界的,因为并非path_list
的所有元素都是长度为3的向量。以下作品:
data_list <- setNames(vector("list", length(path_list)), names(path_list))
l <- lengths(path_list) ## length of each vector
for(i in 1:length(data_list)) {
tmp <- vector("list", l[i])
for (j in 1:l[i])) {
tmp[[j]] <- read.csv(path_list[[i]][j], header=TRUE, na.strings = "", stringsAsFactors = FALSE)
}
data_list[[i]] <- tmp
}