Reputation: 95
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?
Upvotes: 0
Views: 138
Reputation: 1790
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
Upvotes: 3