I have long list of data frames (e.g., 100s) with names d1,d2,d3,..d100. I want to combine them in r as df <- cbind(d1:d100)? is there any efficient way of combining them except writing all column names?
Asked
Active
Viewed 121 times
0
-
`do.call(cbind,d)` – count Apr 28 '17 at 10:29
-
you don't want colnames to be added? you can set `names(df) = NULL` – timfaber Apr 28 '17 at 10:32
-
It is often better to work with lists of data.frames. See gregor's answer [here](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for some tips. – lmo Apr 28 '17 at 11:44
1 Answers
3
You could first pack all your data frames into a list and then cbind
them using do.call
. Here I am assuming your data frames are called d1, d2, ... and that they all have the same number of rows:
## Sample data:
d1 <- data.frame(A = 1:3, B = 4:6)
d2 <- data.frame(C = 7:9)
d3 <- data.frame(D = 10:12, E = 13:15)
## Put them into a list:
myList <- lapply(1:3, function(ii){get(paste0("d", ii))})
## Combine them into one big data frame:
myDataFrame <- do.call('cbind', myList)
myDataFrame
# A B C D E
# 1 1 4 7 10 13
# 2 2 5 8 11 14
# 3 3 6 9 12 15

ikop
- 1,760
- 1
- 12
- 24