问题
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