0

I have a list of list for example (copied from a different post).

ls <- list(a = list(e1 = "value1.1", e2 = "value1.2"),
           b = list(e1 = "value2.1", e2 = "value2.2"))

presently the structure of the list looks likes this:

> lapply(ls, names)
$a
[1] "e1" "e2"

$b
[1] "e1" "e2"

I want the list to be in this format.

$a.e1
$a.e2
$b.e1
$b.e2

i.e the child list merged with the parent list and the list renamed with the parent name followed by the child name

It will be nice if anyone can tell me both using for loop and apply function (if they are required).

Thanks in advance

1 Answers1

3

We can use c with do.call to flatten the list

ls1 <- do.call("c", ls)
names(ls1)
#[1] "a.e1" "a.e2" "b.e1" "b.e2"

unlist would also work if we the recursive is specified as FALSE

unlist(ls, recursive = FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662