Loop read.csv files containing pattern in file name

帅比萌擦擦* 提交于 2020-01-15 09:13:48

问题


I created a vector with 30 words, called "club"

club <- pixid$ack1

Next i want to import 30 csv files. Each filename contains 1 of the words in "club".

for (i in club){
DCM.[i] <- read.csv(list.files(pattern = "[i]"))
}

However I receive the following error:

Error in file(file, "rt") : invalid 'description' argument.

How can I read in all of these files containing names from the vector? I'm hoping this is just a syntax error.


回答1:


It is possible that there are multiple files for a single pattern in the 'club' vector. We loop through the 'club' of patterns, list the files based on that pattern using list.files and then loop through the file names, and read it with read.csv

DCM <- lapply(club, function(x) lapply(list.files(pattern = x), 
               function(x) read.csv(x, stringsAsFactors=FALSE, row.names = NULL)))

The above is a nested list containing a list of data.frames for each pattern provided by the 'club'.



来源:https://stackoverflow.com/questions/41913801/loop-read-csv-files-containing-pattern-in-file-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!