[英]Read, process and export analysis results from multiple .csv files in R
I have a bunch of CSV files and I would like to perform the same analysis (in R) on the data within each file. 我有一堆CSV文件,我想对每个文件中的数据执行相同的分析(在R中)。 Firstly, I assume each file must be read into R (as opposed to running a function on the CSV and providing output, like a sed script).
首先,我假设每个文件都必须读入R中(这与在CSV上运行函数并提供输出(如sed脚本)相反)。
What is the best way to input numerous CSV files to R, in order to perform the analysis and then output separate results for each input? 为了执行分析然后为每个输入输出单独的结果,向R输入大量CSV文件的最佳方法是什么?
Thanks (btw I'm a complete R newbie) 谢谢(顺便说一句,我是一个完整的R新手)
You could go for Sean's option, but it's going to lead to several problems: 您可以选择Sean的选择,但这会导致几个问题:
A better way of doing it might be with lapply()
. 更好的方法可能是使用
lapply()
。
# List files
filelist <- list.files(pattern = "*.csv")
# Now we use lapply to perform a set of operations
# on each entry in the list of filenames.
to_dispose_of <- lapply(filelist, function(x) {
# Read in the file specified by 'x' - an entry in filelist
data.df <- read.csv(x, skip = 1, header = TRUE)
# Store the filename, minus .csv. This will be important later.
filename <- substr(x = x, start = 1, stop = (nchar(x)-4))
# Your analysis work goes here. You only have to write it out once
# to perform it on each individual file.
...
# Eventually you'll end up with a data frame or a vector of analysis
# to write out. Great! Since you've kept the value of x around,
# you can do that trivially
write.table(x = data_to_output,
file = paste0(filename, "_analysis.csv"),
sep = ",")
})
And done. 并做了。
You can try the following codes by putting all csv files in the same directory. 您可以通过将所有csv文件放在同一目录中来尝试以下代码。
names = list.files(pattern="*.csv") %csv file names
for(i in 1:length(names)){ assign(names[i],read.csv(names[i],skip=1, header=TRUE))}
Hope this helps ! 希望这可以帮助 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.