Merge Two Lists in R

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)

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"

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)

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

Merge more than two lists in R

Here's an alternative that works by constructing and then evaluating the same call to mapply() shown in the OP:

do.call(mapply, c(FUN=c, sapply(lsNames, as.symbol), SIMPLIFY=FALSE))
# $A
# file1 file2 file3
# 1 2 3
#
# $B
# file1 file2 file3
# 2 3 4

How to merge two lists in parallel in R?

Using  Map simply:

   Map(list,A,B)

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

R: merge two lists of lists of dataframes

Here is a solution using base R:

x <- c(L1, L2)
lapply(split(x, names(x)), function(i){
xsub <- do.call(c, unname(i))
lapply(split(xsub, names(xsub)), function(j) do.call(rbind, unname(j)))
})
  • split(x, names(x)) will put Q1s together and Q2s together;
  • xsub <- do.call(c, unname(i)) will combine Q1s or Q2s into a list data.frames;
  • split(xsub, names(xsub)) will group data.frames by their names (A, B, C);

The output is:

# $Q1
# $Q1$A
# X1
# 1 1
# 2 2
# 3 3
#
# $Q1$B
# X1
# 1 4
# 2 5
# 3 6
#
# $Q1$C
# X1
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 6
#
#
# $Q2
# $Q2$A
# X1
# 1 4
# 2 5
# 3 6
#
# $Q2$B
# X1
# 1 1
# 2 2
# 3 3
#
# $Q2$C
# X1
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 6


Related Topics



Leave a reply



Submit