我试图在列表中存储多个数据帧。然而,在某些时候,数据框最终会被转换成列表,所以我最终列出了一个列表。将数据帧组合到列表中
我真的很想做的事情就是把所有的数据帧都保存在某种结构中。
下面是失败的代码:
all_dframes <- list() # initialise a list that will hold a dataframe as each item
for(file in filelist){ # load each file
dframe <- read.csv(file) # read CSV file
all_dframes[length(all_dframes)+1] <- dframe # add to the list
}
如果我现在打电话,例如,class(all_dframes[1])
,我得到的名单“,而如果我叫class(dframe)
我得到“data.frame”!
你可以完成'files < - list.files(pattern =“。csv”); lapply(files,function(x)read.csv(x,header = TRUE))' – akrun 2014-11-04 08:56:19
或'library(data.table);文件< - lapply(list.files(pattern =“。csv”),fread)' – zx8754 2014-11-04 09:14:59