Home > Blockchain >  Re-organizing nested list elements in R
Re-organizing nested list elements in R

Time:09-16

I have a long list that has elements including charactor and numeric vectors. I want to re-organize them to the data frame format.

Let's say this is the structure of two first elements of the list_a:

list_a <- list(ENSG00000040608 = list(var = c("chr22_20230714_G_A_b38", 
"chr22_20230737_G_A_b38", "chr22_20231229_T_A_b38", "chr22_20231474_G_A_b38", 
"chr22_20231667_C_G_b38", "chr22_20231957_G_C_b38", "chr22_20231969_G_T_b38", 
"chr22_20232125_G_A_b38", "chr22_20232392_G_A_b38", "chr22_20232643_A_C_b38"
), coeff = c(0.0000953181301665087, -0.00124036704551427, 0.000808061558738542, 
0.000387528601423933, -0.000120624028990859, -0.00119982510044018, 
-0.000120623899338185, -0.000435011907222715, 0.000715410285903684, 
-0.000347899088267475)), ENSG00000040608 = list(var = c("chr22_20230714_G_A_b38", 
"chr22_20230737_G_A_b38", "chr22_20231229_T_A_b38", "chr22_20231474_G_A_b38", 
"chr22_20231667_C_G_b38", "chr22_20231957_G_C_b38", "chr22_20231969_G_T_b38", 
"chr22_20232125_G_A_b38", "chr22_20232392_G_A_b38", "chr22_20232643_A_C_b38"
), coeff = c(0.0000953181301665087, -0.00124036704551427, 0.000808061558738542, 
0.000387528601423933, -0.000120624028990859, -0.00119982510044018, 
-0.000120623899338185, -0.000435011907222715, 0.000715410285903684, 
-0.000347899088267475)))

The desire output as list_b:

   list_b <- list(ENSG00000040608 = structure(list(chr22_20230714_G_A_b38 = -0.000347899088267475, 
    chr22_20230737_G_A_b38 = 0.0000953181301665087, chr22_20231229_T_A_b38 = -0.00124036704551427, 
    chr22_20231474_G_A_b38 = 0.000808061558738542, chr22_20231667_C_G_b38 = 0.000387528601423933, 
    chr22_20231957_G_C_b38 = -0.000120624028990859, chr22_20231969_G_T_b38 = -0.00119982510044018, 
    chr22_20232125_G_A_b38 = -0.000120623899338185, chr22_20232392_G_A_b38 = -0.000435011907222715, 
    chr22_20232643_A_C_b38 = -0.000347899088267475), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame")), ENSG00000040608 = structure(list(
    chr22_20230714_G_A_b38 = -0.000347899088267475, chr22_20230737_G_A_b38 = 0.0000953181301665087, 
    chr22_20231229_T_A_b38 = -0.00124036704551427, chr22_20231474_G_A_b38 = 0.000808061558738542, 
    chr22_20231667_C_G_b38 = 0.000387528601423933, chr22_20231957_G_C_b38 = -0.000120624028990859, 
    chr22_20231969_G_T_b38 = -0.00119982510044018, chr22_20232125_G_A_b38 = -0.000120623899338185, 
    chr22_20232392_G_A_b38 = -0.000435011907222715, chr22_20232643_A_C_b38 = -0.000347899088267475), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame")))

Also I need to have those numbers as integer.

I would appreciate your guidance.

CodePudding user response:

base R

lapply(list_a, function(x) {
  df <- as.data.frame(t(x[[2]]))
  colnames(df) <- x[[1]]
  df
  })

tidyverse

library(tidyverse)
map(list_a, ~ .x %>% 
      as_tibble() %>% 
      pivot_wider(names_from = "var", values_from = "coeff"))
  • Related