Home > Blockchain >  r : convert list to tribble or dataframe
r : convert list to tribble or dataframe

Time:01-13

stackoverflow! I would like to reorganize data below, so that

structure(list(A = list(structure(c(0.0208625254600925, 0.484137748685967
), conf.level = 0.95)), B = list(structure(c(0.0165759134400838, 
0.404602696603372), conf.level = 0.95)), C = list(structure(c(0.102983550772617, 
0.435400345988533), conf.level = 0.95)), D = list(structure(c(0.0630000065376768, 
0.380829879093994), conf.level = 0.95))), class = "data.frame", row.names = "conf.int")
tribble(
  ~A, ~B, ~C, ~D,
0.0208625254600925, 0.0165759134400838, ..
0.484137748685967, 0.404602696603372, ..

)

How can I achieve this? Thank you very much!

CodePudding user response:

Your tribble example is unclear: I assume you want 4 columns (A-D), and 2 rows with the lower value in row 1, the upper in row 2.

One solution using tidyr::unnest to generate the new rows:

library(tidyr)
library(dplyr)

dataset %>% 
  unnest(cols = everything())

Result:

# A tibble: 2 × 4
       A      B     C      D
   <dbl>  <dbl> <dbl>  <dbl>
1 0.0209 0.0166 0.103 0.0630
2 0.484  0.405  0.435 0.381

Data:

dataset <- structure(list(A = list(structure(c(0.0208625254600925, 0.484137748685967
), conf.level = 0.95)), B = list(structure(c(0.0165759134400838, 
0.404602696603372), conf.level = 0.95)), C = list(structure(c(0.102983550772617, 
0.435400345988533), conf.level = 0.95)), D = list(structure(c(0.0630000065376768, 
0.380829879093994), conf.level = 0.95))), class = "data.frame", row.names = "conf.int")
  •  Tags:  
  • r
  • Related