What would be an easy and fast way to get from:
x <- list(a1=2, b1=c(1,2), c1=1:3)
y <- list(a2=5, b2=c(2,5), c2=2:4)
to
list(list(x$a1, y$a2), list(x$b1, y$b2), list(x$c1, y$c2))
?
Or in general:
If list x and y have same length and their elements also correspond in length, how can it be combined to a single list as shown above?
R provided two inbuilt functions named c() and append() to combine two or more lists. c() function in R language accepts two or more lists as parameters and returns another list with the elements of both the lists.
To combine data frames stored in a list in R, we can use full_join function of dplyr package inside Reduce function.
The simplest way to group together values is with the function c() . Feel free to refer to this function however you like, but the words concatenate, combine, and collect are all good options.
An easy way would be to use mapply
as follows:
mapply(x, y, FUN=list, SIMPLIFY=FALSE)
Not sure if it's the fastest though. You can replace x
and y
with unname(x)
and unname(y)
if you don't want names in your output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With