繁体   English   中英

如何读取R中的每个.csv文件并将其导出为单个大文件

[英]How to read every .csv file in R and export them into single large file

嗨,我有以下格式的数据

101,20130826T155649
------------------------------------------------------------------------
3,1,round-0,10552,180,yellow
12002,1,round-1,19502,150,yellow
22452,1,round-2,28957,130,yellow,30457,160,brake,31457,170,red
38657,1,round-3,46662,160,yellow,47912,185,red

我一直在阅读它们,并通过此代码对其进行清洁/格式化

b <- read.table("sid-101-20130826T155649.csv", sep = ',', fill=TRUE, col.names=paste("V", 1:18,sep="") )
b$id<- b[1,1]
b<-b[-1,]
b<-b[-1,]
b$yellow<-B$V6

依此类推,大约有300个这样的文件,理想情况下,它们都将在没有前两行的情况下进行编译,因为第一行只是id,我创建了一个单独的列来标识这些数据。 有谁知道如何快速读取这些表并清理和格式化我想要的方式,然后将它们编译成大文件并导出?

您可以使用lapply读取所有文件,清理并格式化它们,并将结果数据帧存储在列表中。 然后使用do.call将所有数据帧组合为单个大数据帧。

# Get vector of files names to read
files.to.load = list.files(pattern="csv$")

# Read the files
df.list = lapply(files.to.load, function(file) {
   df = read.table(file, sep = ',', fill=TRUE, col.names=paste("V", 1:18,sep=""))
   ... # Cleaning and formatting code goes here
   df$file.name = file  # In case you need to know which file each row came from
   return(df)
})

# Combine into a single data frame
df.combined = do.call(rbind, df.list)

暂无
暂无

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

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