[英]How can I save read dataframes as list in R?
I have say about 10 .txt
files in my directory that I read like this: 我说过我的目录中大约有10个
.txt
文件,如下所示:
sampleFiles <- list.files(directory)
for (i in 1:length(sampleFiles)) {
table <- read.table( sampleFiles[i], header = TRUE)
}
I want to store the read file such that I can access them as table1
for i=1
, table2
for i=2
and tablen
for i=n
. 我想存储读取的文件,以便我可以将它们作为
table1
for i=1
, table2
for i=2
和tablen
for i=n
。 How can I read all these files and save as dataframe base names table
? 如何读取所有这些文件并将其另存为数据框基本名称
table
?
Use lapply
使用
lapply
Data <- lapply( list.files(directory), read.table, header=TRUE)
In order to access each data.frame you can use [[
as in Data[[1]]
, Data[[2]]
,..., Data[[n]]
为了访问每个data.frame,您可以使用
[[
如Data[[1]]
, Data[[2]]
,..., Data[[n]]
Read about how to Extract or Replace Parts of an Object using [[
阅读关于如何提取或替换对象的部分使用
[[
To name them as you describe, replace the table <-
assignment in your loop with 要按照您的描述命名,请将循环中的
table <-
分配替换为
assign(paste0("table", j), read.table(sampleFiles[j], header = TRUE))
Your question title is slightly misleading, as this is not saving the tables as a list in the formal R sense of a list (for which, see the other answer). 您的问题标题有点误导,因为这并不是将表格以列表的形式R形式另存为列表(有关此信息,请参见其他答案)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.