Home > Software design >  How to transform one row list of tibbles into list of vectors
How to transform one row list of tibbles into list of vectors

Time:07-30

I have this list with 5 tibbles that each consist of one row:

<list_of<
  tbl_df<
    one  : integer
    two  : integer
    three: integer
    four : integer
  >
>[5]>
[[1]]
# A tibble: 1 x 4
    one   two three  four
  <int> <int> <int> <int>
1     1     3     2     4

[[2]]
# A tibble: 1 x 4
    one   two three  four
  <int> <int> <int> <int>
1     2     0     1     5

[[3]]
# A tibble: 1 x 4
    one   two three  four
  <int> <int> <int> <int>
1     3     2     1     4

[[4]]
# A tibble: 1 x 4
    one   two three  four
  <int> <int> <int> <int>
1     4     9    11    19

[[5]]
# A tibble: 1 x 4
    one   two three  four
  <int> <int> <int> <int>
1     4     3     2     1
list <- structure(list(structure(list(one = 1L, two = 3L, three = 2L, 
    four = 4L), row.names = c(NA, -1L), class = c("tbl_df", "tbl", 
"data.frame")), structure(list(one = 2L, two = 0L, three = 1L, 
    four = 5L), row.names = c(NA, -1L), class = c("tbl_df", "tbl", 
"data.frame")), structure(list(one = 3L, two = 2L, three = 1L, 
    four = 4L), row.names = c(NA, -1L), class = c("tbl_df", "tbl", 
"data.frame")), structure(list(one = 4L, two = 9L, three = 11L, 
    four = 19L), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(one = 4L, two = 3L, three = 2L, 
    four = 1L), row.names = c(NA, -1L), class = c("tbl_df", "tbl", 
"data.frame"))), ptype = structure(list(one = integer(0), two = integer(0), 
    three = integer(0), four = integer(0)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = integer(0)), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))

My desired output should be a list of 5 vectors, something like this:

[[1]]
[1]     1     3     2     4

[[2]]
[1]     2     0     1     5

[[3]]
[1]     3     2     1     4

[[4]]
[1]     4     9    11    19

[[5]]
[1]     4     3     2     1

I tried now pretty long for this (first thought simply question). But I was not able to solve it.

CodePudding user response:

You can unlist the tibbles.

lapply(list, unlist, use.names = FALSE)

#[[1]]
#[1] 1 3 2 4

#[[2]]
#[1] 2 0 1 5

#[[3]]
#[1] 3 2 1 4

#[[4]]
#[1]  4  9 11 19

#[[5]]
#[1] 4 3 2 1
  • Related