简体   繁体   中英

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

Hi so I have a data in the following format

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

and I have been reading them and cleaning/formating them by this code

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

and so on There are about 300 files like this, and ideally they will all compiled without the first two lines, since the first line is just id and I made a separate column to identity these data. Does anyone know how to read these table quickly and clean and format the way I want then compile them into a large file and export them?

You can use lapply to read all the files, clean and format them, and store the resulting data frames in a list. Then use do.call to combine all of the data frames into single large data frame.

# 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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