0

I have hundreds of csv files in a folder.

temp <- list.files(pattern = "\\.csv$")

for (i in 1:length(temp)) 
  #assign(temp[i], read.csv(temp[i]))
  rbind(temp[i], read.csv(temp[i]))

Same number of columns. Different row names everywhere.

I want to stack them row wise.

How can I do it?

Any solutions are welcome.

example 1:

structure(list(X1.000000 = 2, X2116.000000 = 2120, X9.000000 = 19, 
    X0.624003 = 0.966611, X0.514702 = 1.003793, X0.376078 = 0.03738, 
    X.0.235598 = -0.033804, X0.000490 = 0.007754, X9.632849 = 236.345147, 
    X239017.279109 = 8732111.450485, X31533.351601 = 31533.351601, 
    X1190693206.447250 = 1190693206.44725), .Names = c("X1.000000", 
"X2116.000000", "X9.000000", "X0.624003", "X0.514702", "X0.376078", 
"X.0.235598", "X0.000490", "X9.632849", "X239017.279109", "X31533.351601", 
"X1190693206.447250"), class = "data.frame", row.names = c(NA, 
-1L))

example 2:

structure(list(X1.000000 = c(2, 3), X2076.000000 = c(2087, 2088
), X19.000000 = c(18, 1), X0.995502 = c(1.023801, 1.399615), 
    X0.951066 = c(0.99377, 0.111436), X0.020733 = c(0.026184, 
    0.622115), X.0.017602 = c(0.015609, 0.582765), X0.040000 = c(0.064003, 
    2e-06), X1179.116439 = c(1940.301862, 0.06376), X40779688.793149 = c(68889876.093994, 
    2669.598384), X29610.941343 = c(29610.941343, 29610.941343
    ), X1035102354.289360 = c(1035102354.28936, 1035102354.28936
    )), .Names = c("X1.000000", "X2076.000000", "X19.000000", 
"X0.995502", "X0.951066", "X0.020733", "X.0.017602", "X0.040000", 
"X1179.116439", "X40779688.793149", "X29610.941343", "X1035102354.289360"
), class = "data.frame", row.names = c(NA, -2L))

TRIAL

mymergeddata <- 
  do.call(rbind,
          lapply(temp,read.csv)

Error in match.names(clabs, names(xi)) : 
  names do not match previous names

Trial #2

lst <- lapply(lst, "names<-", paste0("V", 1:(ncol(lst[[1]])-1)))
lst <- lapply(lst, "names<-", paste0("V", 1:ncol(lst[[1]])))
lst2<-do.call(rbind, lst)

This is the error message:

Error in lapply(lst, "names<-", paste0("V", 1:ncol(lst[[1]]))) : 
  'names' attribute [12] must be the same length as the vector [11]

Here:

length(lst[[1]])
[1] 12
maximusdooku
  • 5,242
  • 10
  • 54
  • 94
  • http://stackoverflow.com/questions/23995384/read-and-rbind-multiple-csv-files – Gopala Dec 07 '16 at 03:22
  • 1
    I'm pretty sure you need `header = FALSE` in `read.csv`; it looks like you're reading in the first row of data as column names. – alistaire Dec 07 '16 at 03:52
  • 1
    `df = lapply(temp, read.csv); df = do.call(rbind, df)`. The first statement reads all of your csv files into a list of data frames. The second statement stacks them into a single data frame. This method assumes column names are the same in each data frame. – eipi10 Dec 07 '16 at 06:28
  • @alistaire Can you show me what I am doing wrong? for (i in 1:length(temp)) rbind(temp[i], read.csv(temp[i], row.names = F)) – maximusdooku Dec 07 '16 at 06:41
  • @eipi10 column names are not the same in my case... – maximusdooku Dec 07 '16 at 07:33

1 Answers1

0

Maybe unify the names so that actual names do match previous names:

# ex1 <- structure(list(X1.000000 = 2, X2116.000000 ...
# ex2 <- structure(list(X1.000000 = c(2, 3),...
lst <- list(ex1, ex2) # <=> lst <- lapply(temp,read.csv)
lst <- lapply(lst, "names<-", paste0("V", 1:ncol(lst[[1]])))
do.call(rbind, lst)
#   V1   V2 V3       V4       V5       V6        V7       V8         V9          V10      V11        V12
# 1  2 2120 19 0.966611 1.003793 0.037380 -0.033804 0.007754  236.34515  8732111.450 31533.35 1190693206
# 2  2 2087 18 1.023801 0.993770 0.026184  0.015609 0.064003 1940.30186 68889876.094 29610.94 1035102354
# 3  3 2088  1 1.399615 0.111436 0.622115  0.582765 0.000002    0.06376     2669.598 29610.94 1035102354

This assumes that every file has the same number of columns.

lukeA
  • 53,097
  • 5
  • 97
  • 100