0

I'm having trouble combining 40 different csv files into one. They all have the same variables var 1 var2 and id. but when I combine them I get 2 columns with all the variables in one column. I'm not sure why this is happening, this is my code:

#Combine all files into one file
final = list.files(pattern="*.csv")


for (i in 1:length(final)) {
  assign(final[i], read.csv(final[i]))
}
allfiles = lapply(final,read.delim)

#Have to install plyr and reshape packages for rbind.fill
#Bind matrices by row and fill in missing columns with NA
FilledFiles <- do.call(rbind.fill, allfiles)

#Save combined dataset as Final_Data
write.csv(FilledFiles, file="Final_Data.csv")
missB
  • 5
  • 4
  • 1
    keep the data.frames in a list, dont try to assign them to the global environment, something along the lines of `do.call(rbind.fill, lapply(final, read.csv))` – Rorschach Jul 29 '15 at 01:08
  • 1
    Please supply a short example, with two representative data frames. With what you have, I'm not sure that anyone can give a good answer. – Matthew Lundberg Jul 29 '15 at 01:15
  • Each of the 40 data sets i have are of different lengths, and I'm trying to combine them all into one single data set and I need to grow it because my output right now looks like – missB Jul 29 '15 at 01:29
  • 1
    `write.csv(rbindlist(lapply(list.files(pattern="*.csv"),fread)),file="output.csv",row.names=F)` – MichaelChirico Jul 29 '15 at 01:31
  • It doesn't matter how many data sets in your real problem, you should still model it with just two or three small data sets that show the problem. And you should include the data in the question itself. – Matthew Lundberg Jul 29 '15 at 01:42
  • The length of a dataframe is the number of columns, not the bombed of rows. – IRTFM Jul 29 '15 at 06:12

1 Answers1

0

I don't see the point of reading all data into R just to write the combined data to a new file. That's a task your OS is doing for you:
Windows
Linux

Community
  • 1
  • 1
MarkusN
  • 3,051
  • 1
  • 18
  • 26