rstudio - How to read every .csv file in R and export them into single large file -
hi have data in 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 have been reading them , cleaning/formating them 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 on there 300 files this, , ideally compiled without first 2 lines, since first line id , made separate column identity these data. know how read these table , clean , format way want compile them large file , export them?
you can use lapply
read files, clean , format them, , store resulting data frames in list. use do.call
combine of data frames single large data frame.
# vector of files names read files.to.load = list.files(pattern="csv$") # read files df.list = lapply(files.to.load, function(file) { df = read.table(file, sep = ',', fill=true, col.names=paste("v", 1:18,sep="")) ... # cleaning , formatting code goes here df$file.name = file # in case need know file each row came return(df) }) # combine single data frame df.combined = do.call(rbind, df.list)
Comments
Post a Comment