cbind two lists of data.frames to a new list [duplicate]
Asked Answered
D

2

8

I have two lists of data.frames. Both lists have the same length and contain fitting data.frames in their according list elements. So the scenario looks like this

dfa = data.frame(a=1:3, b = letters[1:3])
dfb = data.frame(x=runif(3))
a = replicate(3, dfa, simplify = FALSE)
b = replicate(3, dfb, simplify = FALSE)

One obvious solution is:

lapply(seq_along(a), function(i) cbind(a[[i]], b[[i]]))

But I was wondering if their might be a better solution.

Diverge answered 6/3, 2015 at 11:55 Comment(0)
E
16

You can use Map

Map(cbind, a, b)
Existential answered 6/3, 2015 at 12:1 Comment(0)
R
4

You can use mapply :

mapply(cbind, a, b, SIMPLIFY=F)
Randallrandan answered 6/3, 2015 at 12:4 Comment(1)
Map is kind of a wrapper for mapply(FUN = f, ..., SIMPLIFY = FALSE) Check the MapExistential

© 2022 - 2024 — McMap. All rights reserved.