Assume I have a list of lists as bellow. I want to keep the second and fourth element of each list, IN a format of a list. Basically, I want to do what
rapply(eig_inter_list, function(x) x[c(2,4)])
does but keep the elements of each list within the list itself. The output would be a list of lists but in each list I would have the second and fourth element, instead of all 57. This is a sample of what my data looks like.
dput(eig_inter_list[c(1,2,3)])
list(structure(c(0.314642768055768, 0.325549276750512, 0.32407558151974,
0.503037906331374, 0.177702652110427, 0.649874624573978, 0.22304809425227,
0.19744998358554, 0.322951763103893, 0.417206897701431, 0.378171649699382,
0.25116873932351, 0.485979263395877, 0.411851682123569, 1, 0.478943307151144,
0.710983939873711, 0.205890770051514, 0.335737092407451, 0.495130707288105,
0.395609776015166, 0.367388748844004, 0.236987996918897, 0.17727518450146,
0.542503233906977, 0.22322071021728, 0.305512801458551, 0.703463566780157,
0.0565731620746889, 0.281865665377428, 0.34675705775566, 0.240102820697777,
0.847955796670962, 0.467145199252136, 0.408110899113892, 0.121168347150886,
0.166279979571487, 0.225178903127632, 0.836003339903779, 0.298624839828843,
0.156832369183448, 0.331724029229045, 0.28607359410209, 0.438251331553188,
0.531970745173967, 0.473559658632999, 0.339927590064139, 0.329554185654697,
0.23010077174135, 0.444399306655095, 0.265064970418036, 0.54141477127972,
0.181034728719507, 0.217971681487819, 0.702604336646787, 0.260277784525433,
0.9313793258404), .Dim = c(57L, 1L)), structure(c(0.758053727134031,
0.389169917077854, 0.553155900977713, 0.680957693695371, 0.688478238016961,
0.5406614343729, 0.654085903776063, 0.249245017029272, 0.527924916117291,
0.607686315557293, 0.328402935290058, 0.472653172631753, 0.503447923078263,
0.540956133924371, 0.367957509649887, 0.537869789262281, 0.177185397396901,
0.49323257867289, 0.380042131111929, 0.76085241271694, 0.540174821432495,
0.283063101888133, 0.319997654939547, 0.651366853994039, 0.706918785162937,
0.578280317866076, 0.871853266130338, 0.874076858150783, 1, 0.611669207162514,
0.28036164071929, 0.292694661837052, 0.610126478002428, 0.380386438921713,
0.669982812090512, 0.122065848180652, 0.974400986291088, 0.945135694392546,
0.462456530300744, 0.134501132230783, 0.322784038090134, 0.317780702999941,
0.662847059747895, 0.523285813371882, 0.668825630786058, 0.79637143229052,
0.379229953581731, 0.282559418794954, 0.745322058370275, 0.697765403065504,
0.282171232377215, 0.672824204974444, 0.812732702343435, 0.747236048953355,
0.810360825374334, 0.147736041262474, 0.638551018944304), .Dim = c(57L,
1L)), structure(c(0.580650487050986, 0.226493031789664, 0.216153469107205,
0.679582322890881, 0.585765696317977, 0.277602052620714, 0.301697850942933,
0.539494048925467, 0.22052504674914, 0.279450501649262, 0.292210734005604,
0.406706750636561, 0.253178327897084, 0.26902431345799, 0.72080329402394,
0.538483400997092, 0.132513153071638, 0.169267787671607, 0.355070307460915,
0.420987061713029, 0.355905556319682, 0.423354094728718, 0.225064747308464,
0.368813823547299, 0.290838727568061, 0.191959913771819, 0.412085260195494,
0.220140351021593, 0.251950925564949, 0.360732509927935, 0.130353223587661,
0.355856853738614, 0.399558273021718, 0.351244298106977, 0.280432050072677,
0.51121978973842, 0.300682631494935, 0.39923136729231, 0.217749058297645,
0.347719424129099, 0.28916475155952, 0.531747942212439, 0.384583756749702,
0.19911455191363, 0.574044809883757, 0.206919558647874, 1, 0.327370421244658,
0.561049901898945, 0.177575568867026, 0.311627824832627, 0.214432911950344,
0.405893310593152, 0.328365875498285, 0.403268555194588, 0.21904514354561,
0.243848932866513), .Dim = c(57L, 1L)))
CodePudding user response:
By default i.e. if the how
argument is not specified, it unlist
according to ?rapply
rapply(object, f, classes = "ANY", deflt = NULL, how = c("unlist", "replace", "list"), ...)
Finally, if how = "unlist", unlist(recursive = TRUE) is called on the result.
Specify the how
to "list" and it should work
rapply(eig_inter_list, function(x) x[c(2,4)], how = "list")