0

I have a lot of csv files with same rownames and colnames, I want combine them by row names, however, all of the examples I found just did it like stacking the data vertically.

for example what I can do now:

filenames <- list.files("C:/Users/Desktop/GCMS_experiment/Raw TIC_GCMS_experi", pattern=".csv", full.names=TRUE)
data <- rbindlist(lapply(filenames,fread))

then output this "Read 93.0% of 4323116 rows Read 4323116 rows and 5 (of 5) columns", this stacking my data, because for one csv file, I have 5 columns and 20684 rows (209 csv files in total).

what I want is combine them by rows,i.e gave me a final csv file containing 20684 rows, 209*5 columns. i.e combine data horizontally not stacking data vertically.

what should I do for the code? thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yuan.guo
  • 41
  • 1
  • 1
  • 5
  • you want to cbind the data and need 20684 columns ? your question is not clear – Hunaidkhan Dec 10 '18 at 09:08
  • `do.call(cbind, lapply(filenames, fread))` maybe? Be aware that this might blow up your memory. – LAP Dec 10 '18 at 09:09
  • Hi thanks. But it did the same as my code. Do you have other solution? – Yuan.guo Dec 10 '18 at 09:22
  • You should try `rbind` instead or `cbind` in LAP's code. –  Dec 10 '18 at 14:42
  • Because you are using `fread` to import the csv files, they are being imported as a `data.table`. The `data.table` implementation has some very strict rules to maintain its rigid framework. One problem this appears to cause involves `cbind`. See https://cran.r-project.org/web/packages/data.table/vignettes/datatable-faq.html#ive-noticed-that-basecbind.data.frame-and-baserbind.data.frame-appear-to-be-changed-by-data.table.-how-is-this-possible-why. Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example in order to let us better try and help you. – hmhensen Jan 05 '19 at 22:32

0 Answers0