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
.