How to combine two lists in R
c
can be used on lists (and not only on vectors):
# you have
l1 = list(2, 3)
l2 = list(4)
# you want
list(2, 3, 4)
[[1]]
[1] 2
[[2]]
[1] 3
[[3]]
[1] 4
# you can do
c(l1, l2)
[[1]]
[1] 2
[[2]]
[1] 3
[[3]]
[1] 4
If you have a list of lists, you can do it (perhaps) more comfortably with do.call
, eg:
do.call(c, list(l1, l2))
Merge Two Lists in R
If lists always have the same structure, as in the example, then a simpler solution is
mapply(c, first, second, SIMPLIFY=FALSE)
Function to combine multiple lists of lists into a single list of lists?
Use unlist
, non-recursive on your initial list.
unlist(l, recursive=FALSE)
# [[1]]
# [1] 1 2 3
#
# [[2]]
# [1] 4 5 6
#
# [[3]]
# [1] 7 8 9
#
# [[4]]
# [1] 10 11 12
#
# [[5]]
# [1] 13 14 15
#
# [[6]]
# [1] 16 17 18
How to merge two list in one alternating elements from both
You can use cbind
then t
and finaly unlist
.
x <- list(sd = list(a=1, b=2, c=3), ld = list(a=4, b=5, c=6))
unlist(t(do.call(cbind, x)))
#do.call(cbind, x) |> t() |> unlist() #Alternative
#[1] 1 4 2 5 3 6
In case there are different types:
unlist(asplit(do.call(cbind, x), 1), FALSE, FALSE)
R merge two lists into one, drawing elements from each list alternatively
Using Map
append the corresponding list
elements of 's1' and 's2' as a list
and then with do.call(c
, flatten the nested list to a list of depth 1.
do.call(c, Map(list, s1, s2))
Or another option is to rbind
the list
elements into a matrix
and remove the dim
attributes with c
c(rbind(s1, s2))
Merge two lists in order to get charaster list in r
If the two vectors are of the same length and in the same order, we can use setNames
new_color_list <- setNames(color_list, names_list)
new_color_list
A B C
"blue" "red" "green"
or names
on the same object
names(color_list) <- names_list
Now, we can concatenate
c(new_color_list, c(D = 'grey'))
A B C D
"blue" "red" "green" "grey"
or assign new element
new_color_list['D'] <- 'grey'
new_color_list
A B C D
"blue" "red" "green" "grey"
combining data frames from two lists
It looks like this is what you want:
map2(x, y, ~ inner_join(.x, .y))
[[1]]
i x z x1 z1
1 1 0.7715183 -0.6933826 -0.3335239 0.5957587
2 2 -0.3824746 -0.7248827 -1.6736241 -1.2248904
3 3 0.3412777 -0.3711940 0.9334678 0.4043867
4 4 -0.4225862 -1.6653314 1.0369985 1.1808140
5 5 0.7468157 0.1704126 -0.1470796 -1.6237296
[[2]]
i x z x1 z1
1 1 0.69264103 -0.6640663 -0.2253319 0.26323254
2 2 -0.07861775 0.7914119 0.3725911 0.02854667
3 3 -0.86588724 -0.5519633 -1.5114177 -0.14283509
4 4 1.16069947 1.1299540 -0.4207173 -1.15829758
5 5 2.13867104 -0.9668079 0.1082068 -2.74714297
How can I combine two lists of lm models element-wise?
I think Map
will give you what you are looking for.
fits1 <- list("fit1", "fit2", "fit3")
fits2 <- list("fit4", "fit5", "fit6")
Map(list, fits1, fits2)
Result:
[[1]]
[[1]][[1]]
[1] "fit1"
[[1]][[2]]
[1] "fit4"
[[2]]
[[2]][[1]]
[1] "fit2"
[[2]][[2]]
[1] "fit5"
[[3]]
[[3]][[1]]
[1] "fit3"
[[3]][[2]]
[1] "fit6"
In tidyverse
speak, that would use purrr
.
library(purrr)
map2(fits1, fits2, list)
Create a list by combining two vectors element-wise
You use Map
-
Map(list, a, b)
#[[1]]
#[[1]][[1]]
#[1] 1
#[[1]][[2]]
#[1] "a"
#[[2]]
#[[2]][[1]]
#[1] 2
#[[2]][[2]]
#[1] "b"
#[[3]]
#[[3]][[1]]
#[1] 3
#[[3]][[2]]
#[1] "c"
Or map2
in purrr
-
purrr::map2(a, b, list)
R: combine two lists with same column names
Here is one option with Map
where we concatenate (c
) the corresponding list
elements to a single one
Map(c, List1, List2)
Related Topics
Removing One Tablegrob When Applied to a Box Plot with a Facet_Wrap
Merge Panel Data to Get Balanced Panel Data
Repeat Vector When Its Length Is Not a Multiple of Desired Total Length
R Fuzzy String Match to Return Specific Column Based on Matched String
Different Breaks Per Facet in Ggplot2 Histogram
R: How to Rbind Two Huge Data-Frames Without Running Out of Memory
Apply a Function to a Subset of Data.Table Columns, by Column-Indices Instead of Name
Suggestions for Speeding Up Random Forests
Selecting Columns in R Data Frame Based on Those *Not* in a Vector
How to Convert Time (Mm:Ss) to Decimal Form in R
Set the Size of Ggsave Exactly
How to Programmatically Extract/Unzip a .7Z (7-Zip) File with R
Apply a Ggplot-Function Per Group with Dplyr and Set Title Per Group
In R Data.Table, How to Pass Variable Parameters to an Expression