1

I want to combine multiple sets of two data frames (a & a_1, b & b_1, etc.). Basically, I want to do what this question is asking. I created a list of my two data sets:

# create data 
a <- c(1, 2, 3)
b <- c(2, 3, 4)
at0H0 <- data.frame(a, b)

c <- c(1, 2, 3)
d <- c(2, 3, 4)
at0H0_1 <- data.frame(c, d)

e <- c(1, 2, 3)
f <- c(2, 3, 4)
at0H1 <- data.frame(a, b)

g <- c(1, 2, 3)
h <- c(2, 3, 4)
at0H1_1 <- data.frame(c, d)

# create lists of names
names <- list("at0H0", "at0H1")
namesLPC <- list("at0H0_1", "at0H1_1")

# column bind the data frames?
dfList <- list(cbind(names, namesLPC))
do.call(cbind, dfList)

But now I need it to create data frames for each. This do.call function just creates a list of the names of the data frames. Thanks!

(Edited to make reproducible code)

Community
  • 1
  • 1
Lisa
  • 909
  • 2
  • 13
  • 31
  • 1
    The example code is not reproducible and `ls` is a bad choice for a variable name because of the `ls()` function. - Try to clarify your questions. – Nairolf Nov 20 '18 at 00:19
  • 1
    `do.call(cbind, mget(unlist(names)))` for instance should do it with your full example. – thelatemail Nov 20 '18 at 00:47
  • thanks @thelatemail, but I need them to be separate data frames. For example, at0H0 and at0H0_1 need to be in one data frame and at0H1 and at0H1_1 need to be in a separate data frame – Lisa Nov 20 '18 at 00:57

2 Answers2

2

It's not super straight-forward, but with a little editing to a joining function you can get there:

joinfun <- function(x) do.call(cbind, unname(mget(x,inherits=TRUE)))
lapply(Map(c, names, namesLPC), joinfun)
#[[1]]
#  a b c d
#1 1 2 1 2
#2 2 3 2 3
#3 3 4 3 4
# 
#[[2]]
#  a b c d
#1 1 2 1 2
#2 2 3 2 3
#3 3 4 3 4

The Map function pairs up the dataset names as required:

Map(c, names, namesLPC)
#[[1]]
#[1] "at0H0"   "at0H0_1"
#
#[[2]]
#[1] "at0H1"   "at0H1_1"

The lapply then loops over each part of the above list to mget (multiple-get) each object into a combined list. Like so, for the first part:

unname(mget(c("at0H0","at0H0_1"),inherits=TRUE))
#[[1]]
#  a b
#1 1 2
#2 2 3
#3 3 4
#
#[[2]]
#  c d
#1 1 2
#2 2 3
#3 3 4

Finally, do.call(cbind, ...) puts this combined list back into a single data.frame:

do.call(cbind, unname(mget(c("at0H0","at0H0_1"),inherits=TRUE)))
#  a b c d
#1 1 2 1 2
#2 2 3 2 3
#3 3 4 3 4
thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • Thanks so much for your thorough explanation. I'm still having trouble getting the code to assign the sets of data to separate data frames. It has be to in separate data frames because there are different numbers of rows. For example, "at0H0" and "at0H0_1" have 839 rows, but "at0H1" and "at0H1_1" have 583 rows. – Lisa Nov 20 '18 at 14:18
0

I've figured out a way to do it. A few notes: I have 360 data sets that I need to combine, which is why it is i in 1:360. This also names the data sets from an array of the names of the data sets (which is dataNames)

for (i in 1:360){
  assign(paste(dataNames[i], sep = ""), cbind(names[[i]], namesLPC[[i]]))
}
Lisa
  • 909
  • 2
  • 13
  • 31