1

Lets say i have the following data:

data <- list(arr = c(2, 2), ab = list(x = c(4, 4), 
             e = list(f = c(1, 2), ff = 1:3, dd = data.frame(c = 1:3, d = 2:4))))

Goal:

I want to flatten the list elements if they have more than one dimension.

Desired output:

>   list(arr = c(2, 2), ab.x = c(4, 4), ab.e.f = c(1, 2), 
+             ab.e.ff = c(1, 2, 3), ab.e.dd.c = 1:3, ab.e.dd.d = 2:4)
$arr
[1] 2 2

$ab.x
[1] 4 4

$ab.e.f
[1] 1 2

$ab.e.ff
[1] 1 2 3

$ab.e.dd.c
[1] 1 2 3

$ab.e.dd.d
[1] 2 3 4

What i tried:

1)

unlist(data, recursive = FALSE)

2)

continue <- TRUE
lst <- list()
nr <- 1
while(continue){
  lst[[nr]] <- data
  data <- unlist(lst[[nr]], recursive = FALSE)
  continue <- !identical(lst[[nr]], data)
  nr <- nr + 1
}
lst[nr - 2]

3)

Tried to work with length(dim(.)), but its often NULL.

Tlatwork
  • 1,445
  • 12
  • 35
  • 2
    I believe this should definitely help - `rlist::list.flatten(data)`, the approach explained already here [R (purrr) flatten list of named lists to list and keep names](https://stackoverflow.com/questions/49252400/r-purrr-flatten-list-of-named-lists-to-list-and-keep-names) – arg0naut91 Feb 15 '20 at 11:09

0 Answers0