如何在R中组合多个.csv文件? [英] How to combine multiple .csv files in R?
问题描述
我有一个目录包含接近2,000个.csv文件。
I have a directory containing close to 2,000 .csv files.
每个文件具有以下结构(显示500行中的4个):
Each file has the following structure (showing 4 out of 500 rows):
Date;QOF
1 2004-01-04 - 2004-01-10;9
2 2004-01-11 - 2004-01-17;11
3 2004-01-18 - 2004-01-24;13
4 2004-01-25 - 2004-01-31;13
QOF列也是.csv文件的名称,每个文件都有唯一的名称(例如MSTF XQS等)我想从每个.csv文件中的这一列合并到第一个正在读取的.csv文件,它也包含日期变量。换句话说,我想保留第一个文件的所有列,并且只合并所有其他.csv文件的第二列到此文件。最终结果应为:
The column "QOF" is also the name of the .csv file and each file has a unique name (e.g. "MSTF", "XQS" etc.) I would like this column from each .csv file to be merged on to the first .csv file being read which also contains the date variable. In other words I want to keep all columns from the first file and merge only the second column from all other .csv files on to this file. End result should be something like:
Date;QOF;MSTF;XQS
1 2004-01-04 - 2004-01-10;9;10;8
2 2004-01-11 - 2004-01-17;11;11;5
3 2004-01-18 - 2004-01-24;13;31;2
4 2004-01-25 - 2004-01-31;13;45;23
到目前为止,我已经尝试过:
So far I have tried this:
filenames <- list.files()
do.call("cbind", lapply(filenames, read.csv, header = TRUE))
推荐答案
mybig <- do.call( rbind, lapply( listfiles, function(nam){
cbind(name=nam, read.file(paste0(nam,".csv"), header=TRUE) )
}
) )
未测试。并注意到我故意不遵循你建议的结构。我不能thnk一个更混乱的数据结构下来工作。您可能正在考虑将此格式用于输出,并且首先需要构建一个数据帧,然后将其写入带分号分隔符的文件。
Untested. And notice that I intentionally did not follow the structure you suggested. I cannot thnk of a more confusing data structure to work with down the line. You might be thinking of using that format for output and would first need to build a dataframe and then write it to a file with semi-colon delimiter.
这篇关于如何在R中组合多个.csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!