简体   繁体   English

读入多个 .csv 文件

[英]Read into multiple .csv files

I would like to read into csv.我想读入csv. files from a specific location.来自特定位置的文件。 How do I read multiple files into the global environment at once?如何一次将多个文件读入全局环境? How do I unpack the contents of a list of data frames into it, or vice versa?如何将数据框列表的内容解压到其中,反之亦然?

You can read the data in a list.您可以读取列表中的数据。

filenames <- list.files(pattern = 'csv$', full.names = TRUE)
list_data <- lapply(filenames, readr::read_csv)

It is not recommended to create multiple objects in global environment (like df1 , df2 etc) because they are difficult to manage but if you need them you can use list2env不建议在全局环境中创建多个对象(如df1df2等),因为它们很难管理,但如果你需要它们,你可以使用list2env

names(list_data) <- paste0('df', seq_along(list_data))
list2env(list_data, .GlobalEnv)

Not sure if I understand your question properly, does this work?不确定我是否正确理解您的问题,这有效吗?

tables_to_read <- list()

for (i in 1:10) {
     tables_to_read[i] <- read_csv(paste0(i, '.csv'))
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM