Home > Mobile >  Integrate two lists sub-elements in R
Integrate two lists sub-elements in R

Time:10-21

Each element of the list pred has five subelements, each containing a vector of values. Also, list iid contains the same number and length of sub-elements as pred. In order to integrate two lists, I want for each element of pred, the characters in the iid become a column name and the corresponding values in the pred to become rows like a data frame.

## Pred list
pred <- list(ENSG00000141956 = list(structure(c(0.00272564851305302, 
0.00272564851305302, 0.0202754075985887, 0.00676249893692591, 
0.00336835547752925, 0.00336835547752925, 0.0191065491234794, 
0.00272564851305302, 0.00503517684692274, 0.0150945626471007, 
0.00602406792927852, 0.00272564851305302, 0.00272564851305302, 
0.00272564851305302, 0.00300074743348399, 0.00300074743348399, 
0.00327584635391496, 0.00300074743348399, 0.00272564851305302, 
0.00614716297153329, 0.0208907435639813, 0.00879692835036466, 
0.00574896900884754), .Dim = c(23L, 1L), .Dimnames = list(NULL, 
    "s1")), structure(c(-0.0149845730327965, -0.013847280341284, 
-0.0275325515044248, -0.0141988014913753, -0.0136176647967972, 
-0.0275325515044248, -0.0153360941828878, -0.0136116558238996, 
-0.0138532893141817, -0.0151539874321891, -0.0138532893141817, 
-0.027416654871718, -0.0215030592385638, -0.0205524436634252, 
-0.0136116558238996, -0.0174136560129391, -0.0138532893141817, 
-0.0136176647967972, -0.0208273881822836, -0.0136176647967972, 
-0.0250634259373212, -0.0136176647967972), .Dim = c(22L, 1L), .Dimnames = list(
    NULL, "s1")), structure(c(-0.037929571081887, -0.0352737894666712, 
-0.0364115473528404, -0.0353679266239015, -0.0376033957001448, 
-0.0308210759870506, -0.0376033957001448, -0.0376033957001448, 
-0.0420643633134619, -0.0360853719710982, -0.037929571081887, 
-0.0360853719710982, -0.0330565450632938, -0.037929571081887, 
-0.037929571081887, -0.0352737894666712, -0.0350417512421593, 
-0.0376033957001448, -0.0376033957001448, -0.0360853719710982, 
-0.0364115473528404, -0.0381176463621845, -0.037929571081887), .Dim = c(23L, 
1L), .Dimnames = list(NULL, "s1")), structure(c(0.0311974749143465, 
0.0302892560510041, 0.0310530889747851, 0.0253053324056306, 0.0251609464660692, 
0.0250389673364521, 0.0251609464660692, 0.0310530889747851, 0.0253053324056306, 
0.0310530889747851, 0.0360370126201587, 0.0253053324056306, 0.0254497183451919, 
0.0236698466255728, 0.0250389673364521, 0.0302892560510041, 0.02716525500288, 
0.0302892560510041, 0.0250389673364521, 0.0254497183451919, 0.0302892560510041, 
0.0311974749143465), .Dim = c(22L, 1L), .Dimnames = list(NULL, 
    "s1")), structure(c(0.0196357496993677, 0.0167271490318362, 
0.0168263419569211, 0.0199993991223454, 0.0167271490318362, 0.0166279561067514, 
0.0168263419569211, 0.0218122431555806, 0.0169603561059156, 0.0167271490318362, 
0.0168263419569211, 0.0231724562877696, 0.0187383789152412, 0.0167271490318362, 
0.0168263419569211, 0.0121019992430171, 0.0168263419569211, 0.0168980287283542, 
0.0166279561067514, 0.0168263419569211, 0.0199993991223454, 0.0195049035328895, 
0.0199993991223454), .Dim = c(23L, 1L), .Dimnames = list(NULL, 
    "s1"))), ENSG00000141959 = list(structure(c(0.00386794757219419, 
0.00331068544990299, 0.00549727349038067, -0.00220598881051638, 
-0.00220598881051638, -0.00047576052598424, -0.000019400770038708, 
-0.00265461664506892, -0.000205735129200752, -0.00148165048282797, 
-0.00164872668822519, -0.0013787012914417, -0.00265461664506892, 
-0.00164872668822519, -0.00220598881051638, -0.00103302264827544, 
-0.000205735129200752, 0.00659503688105354, 0.00169406666058391, 
0.00659503688105354, 0.00269498140995324, 0.00225132878287511, 
0.00549727349038067), .Dim = c(23L, 1L), .Dimnames = list(NULL, 
    "s1")), structure(c(0.0210024880281395, 0.0171347306001959, 
0.018137007193539, 0.0228033435746737, 0.0205811017648125, 0.0168279586537404, 
0.0205811017648125, 0.0175495081991909, 0.0183843909485424, 0.0211578279565913, 
0.0177114567919747, 0.0204149125032887, 0.0164914915754566, 0.0178224278158885, 
0.0198274900394434, 0.0177114567919747, 0.0180479238702585, 0.0168279586537404, 
0.0214942950348751, 0.0171347306001959, 0.0171347306001959, 0.0246041991212079
), .Dim = c(22L, 1L), .Dimnames = list(NULL, "s1")), structure(c(0.00746714052332263, 
0.00818008340245513, 0.0123095880229919, 0.00943601338470202, 
0.00897187247388116, 0.00795749001625596, 0.00943601338470202, 
0.00780432935650995, 0.00563415692477041, 0.00671924314064018, 
0.00904257623212573, 0.00994736151433579, 0.00971976985656361, 
0.00671924314064018, 0.00941833027807091, 0.00780432935650995, 
0.00977320221788934, 0.00671924314064018, 0.00818008340245513, 
0.00795749001625596, 0.0100569586897509, 0.00746714052332263, 
0.00904257623212573), .Dim = c(23L, 1L), .Dimnames = list(NULL, 
    "s1")), structure(c(-0.0148396051801961, -0.0141597831410679, 
-0.0141750601473065, -0.0141133976650269, -0.012512044067443, 
-0.0141133976650269, -0.0143460749121506, -0.0139271058939441, 
-0.00993393444101102, -0.00920772692584177, -0.0144923036878726, 
-0.00993393444101102, -0.0135798044016206, -0.0148396051801961, 
-0.0150722824273198, -0.0144923036878726, -0.0143060119167898, 
-0.00955502841816534, -0.0141133976650269, -0.0135798044016206, 
-0.00920772692584177, -0.0152185112030418), .Dim = c(22L, 1L), .Dimnames = list(
    NULL, "s1")), structure(c(-0.0151287367580205, -0.0164455761832253, 
-0.016964848400182, -0.0151522926443206, -0.0126712489200476, 
-0.0144246945509821, -0.0151522926443206, -0.0155466417925569, 
-0.0113544094948428, -0.0151287367580205, -0.0142298023673521, 
-0.0164220202969252, -0.0106503672878044, -0.0156480089749772, 
-0.0164220202969252, -0.0140893159157665, -0.0164220202969252, 
-0.0156480089749772, -0.0156715648612773, -0.0170940674370707, 
-0.0142298023673521, -0.0170940674370707, -0.01773885972213), .Dim = c(23L, 
1L), .Dimnames = list(NULL, "s1"))))

## iid list
iid <- list(foldSet_1 = c("GTEX-11EQ9", "GTEX-12WSJ", "GTEX-1EKGG", 
"GTEX-1EU9M", "GTEX-1EWIQ", "GTEX-1F5PK", "GTEX-1GMRU", "GTEX-1GTWX", 
"GTEX-S95S", "GTEX-SN8G", "GTEX-T5JC", "GTEX-T6MO", "GTEX-TKQ2", 
"GTEX-U3ZM", "GTEX-V1D1", "GTEX-WFG7", "GTEX-WFG8", "GTEX-WFJO", 
"GTEX-WOFM", "GTEX-Y5V6", "GTEX-Y8LW", "GTEX-ZC5H", "GTEX-ZXES"
), foldSet_2 = c("GTEX-11EM3", "GTEX-11EMC", "GTEX-11ZTT", "GTEX-1212Z", 
"GTEX-12696", "GTEX-1269C", "GTEX-1GF9U", "GTEX-1GMR8", "GTEX-1I4MK", 
"GTEX-S4Q7", "GTEX-U3ZG", "GTEX-WWTW", "GTEX-WZTO", "GTEX-X4EO", 
"GTEX-XPT6", "GTEX-Y5V5", "GTEX-YB5K", "GTEX-YEC4", "GTEX-ZF3C", 
"GTEX-ZG7Y", "GTEX-ZP4G", "GTEX-ZT9W"), foldSet_3 = c("GTEX-11I78", 
"GTEX-11VI4", "GTEX-12C56", "GTEX-13111", "GTEX-1399R", "GTEX-1FIGZ", 
"GTEX-1GN2E", "GTEX-RUSQ", "GTEX-T5JW", "GTEX-UPJH", "GTEX-V955", 
"GTEX-VUSG", "GTEX-W5WG", "GTEX-WRHK", "GTEX-XBED", "GTEX-XMK1", 
"GTEX-XQ3S", "GTEX-XXEK", "GTEX-Y8DK", "GTEX-ZEX8", "GTEX-ZPCL", 
"GTEX-ZQG8", "GTEX-ZTPG"), foldSet_4 = c("GTEX-1122O", "GTEX-1211K", 
"GTEX-12WSL", "GTEX-T6MN", "GTEX-TMZS", "GTEX-UPK5", "GTEX-WFON", 
"GTEX-WHSB", "GTEX-WHWD", "GTEX-WY7C", "GTEX-WYVS", "GTEX-X15G", 
"GTEX-XGQ4", "GTEX-Y9LG", "GTEX-YEC3", "GTEX-ZDTS", "GTEX-ZDTT", 
"GTEX-ZF2S", "GTEX-ZLV1", "GTEX-ZPIC", "GTEX-ZT9X", "GTEX-ZUA1"
), foldSet_5 = c("GTEX-12WSN", "GTEX-1F48J", "GTEX-1F52S", "GTEX-1GMR3", 
"GTEX-SUCS", "GTEX-U3ZH", "GTEX-U3ZN", "GTEX-WEY5", "GTEX-WH7G", 
"GTEX-X4EP", "GTEX-X4LF", "GTEX-X5EB", "GTEX-XBEW", "GTEX-XLM4", 
"GTEX-Y5LM", "GTEX-Y8E4", "GTEX-YF7O", "GTEX-ZF29", "GTEX-ZTSS", 
"GTEX-ZTTD", "GTEX-ZV6S", "GTEX-ZVT2", "GTEX-ZVZP"))

Thank you.

CodePudding user response:

We may do

library(purrr)
map(pred, ~  map2_dfc(.x, iid, 
    ~ as.data.frame(matrix(.x, ncol = nrow(.x), dimnames = list(NULL, .y)))))

Or with base R

lapply(pred, \(x) do.call(cbind, Map(\(u, v) as.data.frame(matrix(u, 
   ncol = nrow(u), dimnames = list(NULL, v))), x, iid)))
  • Related