In R Combine a List of Lists into One List

in r combine a list of lists into one list

We can use the concatenate function (c) within do.call to flatten the nested list

res <- do.call(c, listoflists)
all.equal(listofvectors, res, check.attributes = FALSE)
#[1] TRUE

Or as @d.b mentioned in the comments, unlist with recursive = FALSE can also work

unlist(listoflists, recursive = 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

R: combine list of lists into a single dataframe

In fact it is a list containing 5 elements. And the first element is a list of dataframes, whereas the other elements 2, 3, 4 and 5 are NULL. so you need to do access the first element of your list:

do.call(rbind, listOfDataFrames[[1]])

Or:

rbindlist(listOfDataFrames[[1]])

Merge 2 list of lists in R

We can use Map

Map(c, total_delta_final, total_TI_final)

combining elements in a list of lists in R

Another base R option using paste

do.call(paste, c(data.frame(t(list2DF(samples_ID))), sep = "_"))

or

do.call(paste, data.frame(do.call(rbind, samples_ID)), sep = "_"))

Combine lists into a single list in R

The obvious solution here is to simply unlist your list:

list(text = unlist(mylist, use.names = FALSE))
#> $text
#> [1] "I love this product"
#> [2] "Amazing service and care given, will visit once again"
#> [3] "Love this product!!"

Created on 2022-07-31 by the reprex package (v2.0.1)

Concatenate list of lists in R

Yes, that is the correct way to do it. On the example above the result would be a vector like:

c("a", "b", "c", "x", "y", "z", "1", "2", "3")

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 list of lists in R

The merge function is for joining data on common column values (commonly called a join). You need to use rbind instead (the r for row, use cbind to stick columns together).

do.call(rbind, pages) # equivalent to rbind(pages[[1]], pages[[2]], ...)
do.call(rbind, pages[lengths(pages) > 0]) # removing the 0-length elements

If you have additional issues, please provide a reproducible example in your question. This code works on this example:

x = list(data.frame(x = 1), NULL, data.frame(x = 2))
do.call(rbind, x)
# x
# 1 1
# 2 2

Merging list of lists with comma separated in R

Actually you are already close to your goal. Maybe the code below can help you

> do.call(rbind, Map(toString, test))
[,1]
[1,] "1"
[2,] "2"
[3,] "3"
[4,] "a, b"
[5,] "d"


Related Topics



Leave a reply



Submit