Tom
Tom

Reputation: 2341

Appending the lists of two lists of lists

I have two lists as follows:

list1 <- list(c(`0` = 0L, `25` = 0L, `100` = 1L, `250` = 1L, `500` = 1L, 
                `1000` = 1L, Infinity = 3L), c(`0` = 0L, `25` = 0L, `100` = 1L, 
                                               `250` = 1L, `500` = 1L, Infinity = 4L))

list2 <- list(c(`0` = 0L, `25` = 0L, `100` = 0L, `250` = 2L, `500` = 1L, 
                `1000` = 1L, Infinity = 3L), c(`0` = 0L, `25` = 0L, `100` = 1L, 
                                               `250` = 1L, `500` = 1L, Infinity = 4L))

I would like to append list2[[1]] to list1[[1]] and append list2[[2]] to list1[[2]]. So that:

list_out <- list(c(`0` = 0L, `25` = 0L, `100` = 1L, `250` = 1L, `500` = 1L, 
                `1000` = 1L, Infinity = 3L, `0` = 0L, `25` = 0L, `100` = 0L, `250` = 2L, `500` = 1L, 
                `1000` = 1L, Infinity = 3L), c(`0` = 0L, `25` = 0L, `100` = 1L, 
                                               `250` = 1L, `500` = 1L, Infinity = 4L, `0` = 0L, `25` = 0L, `100` = 1L, 
                                               `250` = 1L, `500` = 1L, Infinity = 4L))

Could anyone help me explain how should I do this?

Upvotes: 2

Views: 47

Answers (4)

akrun
akrun

Reputation: 887118

Another option is map2

library(purrr)
map2(list1, list2, c)

Upvotes: 1

Rui Barradas
Rui Barradas

Reputation: 76402

Here is another base R solution.

Map(c, list1, list2)

identical(Map(c, list1, list2), list_out)
#[1] TRUE

Upvotes: 2

benson23
benson23

Reputation: 19097

You can use lapply and c().

lapply(1:length(list1), function(x) c(list1[[x]], list2[[x]]))

Or mapply with append or c:

mapply(append, list1, list2)

Output

[[1]]
       0       25      100      250      500     1000 Infinity        0 
       0        0        1        1        1        1        3        0 
      25      100      250      500     1000 Infinity 
       0        0        2        1        1        3 

[[2]]
       0       25      100      250      500 Infinity        0       25 
       0        0        1        1        1        4        0        0 
     100      250      500 Infinity 
       1        1        1        4 

Check if it's identical to your list_out:

identical(lapply(1:length(list1), function(x) c(list1[[x]], list2[[x]])), list_out)
[1] TRUE

identical(mapply(append, list1, list2), list_out)
[1] TRUE

Upvotes: 2

Quinten
Quinten

Reputation: 41285

You can use the base function append:

> append(list1[[1]], list2[[1]])
       0       25      100      250      500     1000 Infinity        0       25      100 
       0        0        1        1        1        1        3        0        0        0 
     250      500     1000 Infinity 
       2        1        1        3 
>   append(list1[[2]], list2[[2]])
       0       25      100      250      500 Infinity        0       25      100      250 
       0        0        1        1        1        4        0        0        1        1 
     500 Infinity 
       1        4 

Upvotes: 0

Related Questions