Pluck on a nested tibble


#1

assuming a nested tibble y

y <- tibble(a=purrr::rerun(10,tibble(x=purrr::rerun(100,data.frame(xx=rnorm(10))))))

is there a way to pluck directly from depth d an element?

in this case i would want to get back a long data.frame of xx's with concatenated id’s from each parent level.


#2

You can use a “special” subset syntax where an numeric vector is used to “walk” through the tree via child positions.

Also is would be good if you provided a reprex rather than a cut/paste and also, if you can, show an example of what you want to produce. See reprex below of access a tree element.

https://www.tidyverse.org/help/


suppressPackageStartupMessages(library(tibble))
y <- tibble(a=purrr::rerun(10,tibble(x=purrr::rerun(100,data.frame(xx=rnorm(10))))))
y[[c(1,3,1,2,1,7)]]
#> [1] 2.263015

#3

thank you. i’m looking for a generalized purrr answer. in reality my tibble is 4gb with 8 levels and many elements


#4

btw @jennybryan … purrr is throwing what looks like a cpp error…

this is coming out in the console, thereby causing the console to be non responsive and forcing a hard restart of the IDE.

my_element <- function(x) x[[2]]$x%>%select(xx)

x <- y%>%pluck('a',my_element)

 1: list(xx = c(-0.722987935798707, 1.69822266398664, -0.22522818910694, 0.647501327822242, 0.915370543818157, -0.271616953017684, -0.365834456833853, 0.341978099909766, -1.3193114864392, 0.295532417450712))
  2: list(xx = c(0.261112053450925, 1.32783949243393, -1.77856008162182, -0.643610282614432, 0.416918216773016, 0.98659355203076, 2.04551780510913, -0.805813990980882, -0.850969014577867, 0.371439654827122))
  3: list(xx = c(-0.39519914118128, -0.611862369475301, -1.96835328349251, 0.69886632746441, 1.96981120902273, -0.901682688576518, -0.0444438058002072, 1.02422286059118, 0.536689199721475, -1.78636734958676))
  4: list(xx = c(1.75922603540022, 0.364265759915301, -1.85557861658869, 0.106373799325331, -0.449871500678539, 0.949600332145157, -1.5323068398346, -0.256096682205376, -0.87638524397926, -1.66580514027202))
  5: list(xx = c(-1.77650394003292, -0.17161785423635, 0.485089435216388, 0.312392573993947, 0.928146406890381, 1.06709884702389, 1.52116633069666, -0.169618781546967, -0.172077841874826, 0.383064619776651))
  6: list(xx = c(-1.024007748094, 0.114530186523915, -0.824724451439822, 0.713712382041655, -0.933027373145934, 0.617616374529717, 0.672847441316716, 0.31527220915485, -1.17115132191636, 1.49341801139562))
  7: list(xx = c(-0.988740818343435, -1.53345928861795, 0.766230226997624, 0.26769988476797, 0.258304006481383, -2.06515579943348, -1.247854125519, 0.91517904144289, 0.443061965496943, 0.165725699905328))
  8: list(xx = c(0.265620810963575, 0.0578930985138561, -0.767869318007162, 0.652294789219855, 2.94527263424229, -1.20329859000404, 1.41553474136021, -2.48630775494388, -0.286286259017723, -1.25660789926352))
  9: list(xx = c(-0.391147118699199, -0.954226727166825, 0.440602782657643, -2.11575197558607, -0.117205855304783, 2.1746221271999, -0.401014789593113, -1.88511745684185, 0.607676559360339, 0.0969562228378538))
 10: list(xx = c(-1.14006634556746, -1.0492454185674, 1.11207278514766, -0.805651906862494, 1.21236456988217, -0.764087593494259, -0.0289968775304362, -0.11616074726811, 0.811258357916598, -0.477576006840661))
 11: list(xx = c(0.203952420027793, -0.487834541246598, -0.467714418105778, 0.284331202695352, -0.190954176179598, -0.108206060689393, -1.19864438110628, 0.605093882037976, -0.479110183222485, 0.588770475962041))
 12: list(xx = c(1.90997225707674, -2.25236455595367, -0.605194308512999, -0.202235064002255, -1.65272545434211, -0.417195287320829, 1.53950549666283, 1.62870918777035, -0.205972886249866, -0.764331805128122))
 13: list(xx = c(0.295004911054119, -0.480438282639301, 0.893258998621759, 1.27040242314474, 0.223226207115419, 0.826365883667537, -0.0999462521222989, 0.954983160000967, -0.371452716412221, -0.267890265506078))
 14: list(xx = c(1.35253284289337, 0.501208916313415, -1.41962079299234, -0.64553905171778, 0.69939461868283, 0.487768400818186, 0.139879384497438, 0.149487082192384, 0.19040648273556, -0.965614978198357))
 15: list(xx = c(-1.35552529522284, 1.28565153737248, 0.109170332136203, 0.358754795584474, -0.156609619904622, 0.867757124889474, 3.0055428919597, 0.742369390516256, -0.716029657514329, -0.989080162343057))
 16: list(xx = c(0.938785871811544, 0.838136755948468, 1.27313048976208, 0.013415325001743, 0.221693090695242, -1.15452910242638, 0.801156292435347, 0.598920739455895, 0.241925867276127, 1.16453027391798))
 17: list(xx = c(1.43478640887807, 0.724621356217338, -1.06588070373798, -0.613325149297751, 1.05806493969093, 1.72535344457173, 1.421501933855, 1.27843577871574, -0.330111629754117, -0.997964068000685))
 18: list(xx = c(-0.00290385908782232, -2.12048237601445, -1.4355938909246, -1.11844974335347, -0.982035312284209, -0.86082128729494, 0.448693375597564, 1.22486379100138, 0.422106192432351, 0.847954840041882))
 19: list(xx = c(-0.482787932813301, 0.600180656319514, -0.201614563428675, -1.35227049789987, -0.389057478856188, 1.42409438122745, 1.31977994430073, -0.549169635145202, 0.0604283847103742, -0.604075161450281))
 20: list(xx = c(-1.06094822369398, 0.411274246804937, -0.0527868390095146, 1.0852265676628, -0.715115610437822, -0.989749044822864, 0.830390549034231, -1.47149339469037, -0.831608988636831, 0.383133149230451))
 21: list(xx = c(0.565961465008273, 0.0313840129775128, 1.61613582025, -1.02681582888262, 2.31820236638782, 0.391446330233408, 2.24501505300378, 0.923051705961039, -0.484097145982025, 0.767174106451046))
 22: list(xx = c(-0.00827069772191391, 0.317988362556413, -0.630772107723635, 0.125551271277013, 0.472740384428667, -0.563712414927351, -1.58743250902263, 1.44815331486939, 0.274213132352245, 1.02678786677994))
 23: list(xx = c(0.812725325568621, 0.985210513768234, -0.158105144792672, -0.602015316440619, -1.17310114122461, 1.21358500355235, -1.28865926044589, -1.54203419184533, 0.397789909573898, -0.476462110287924))
 24: list(xx = c(1.74169062005194, 0.571922116621342, -2.7726742628582, -0.526600745028154, 0.758788491815443, -0.134962666276115, 1.31540142031198, 1.15955872125663, 2.96372653958687, -1.68525117142215))
 25: list(xx = c(1.57636701830341, 0.504664368055145, 0.419732660047894, 0.261603038602558, 1.46749333384671, -3.82953057423732, 0.855468129212577, -0.539267637118254, -0.014270115214545, -0.123527415994174))
 26: list(xx = c(0.593030285219711, 2.17689726239559, 1.44289419416283, -0.188313216505021, -1.61221702120957, 1.41246459996594, -0.305638517118603, -1.69334360496839, 0.0113794465216422, 1.08750972089741))
 27: list(xx = c(1.25240359350719, -0.406949378726432, -0.229008168480507, -0.646597189435444, -0.811914864547623, -1.00298277079183, 0.681973832771643, -0.4518360256931, 0.00310580332433496, 0.704808032654634))
 28: list(xx = c(-0.886674349471581, 0.154384508814453, -1.42203569428469, -1.50677810164748, 0.43740731567155, 0.153171663415172, -1.06080607696725, 2.16495618875161, 0.934915526390949, 0.429588513723876))
 29: list(xx = c(-0.724817735967617, -2.65013944763642, 1.32751320303743, 0.803575321766148, -1.68271466475523, -1.04399745679009, -0.130994315057753, 0.644058439107223, -1.41272011683908, -1.92415837087826))
 30: list(xx = c(0.612980669314274, 0.27377632374705, -0.253925259506836, -0.643222707734503, -0.484405933431223, 0.198024738060751, -0.410883379485002, -1.46424851412397, 0.643604454946234, 0.499623978873053))
 31: list(xx = c(1.31562693014248, 0.611998173405837, 1.40035368367204, 0.356210128714993, -0.674855193752984, -0.0501244852820587, 0.315140133385068, 0.169344861594965, 0.332927401927769, 0.456349708748955))
 32: list(xx = c(-0.887506230246752, 1.05350739975242, -1.79466137241076, -1.1692101407814, -1.4080147623343, 1.23571804533754, 0.236507780424078, -0.632035644448471, 1.86340386761787, -0.75822581257659))
 33: list(xx = c(0.0264662968374559, -0.695572590204159, 0.444031043553152, -0.551293494927919, -0.397404708465629, 1.69385417102775, 0.478253446069405, 0.251310164789657, 0.339971017465414, -0.389605448449609))
 34: list(xx = c(-1.21178649740226, 0.430050236447923, 0.680951706760051, -0.466897105674773, 1.07933563614902, -0.148958700089812, 1.43772116355284, 0.784310780304814, -0.471997654389291, -0.0924382808995628))
 35: list(xx = c(2.08241646474501, -1.30278781853624, -0.0858132777662443, -0.0639266875960126, -0.584654881805264, 0.926492999713144, 0.140494285622827, 0.419624887073604, 0.233316885706951, -0.181777719230887))
 36: list(xx = c(-1.05963363405264, 0.538212572978883, 0.346379690931336, -1.49841468981412, -0.447936945499549, 1.58677659902027, -0.260736680616062, 0.70000905359118, 0.753265578786017, 0.16295182980808))
 37: list(xx = c(0.462139227145956, -0.175929550108305, 1.28403611331112, -2.2677621277049, -0.135273422916349, -1.86209656337177, -0.137074776599195, 1.65357614782243, 0.589594558869026, -0.161166797293712))
 38: list(xx = c(-0.0162261423491478, 0.0392496489867293, -1.20155253317122, 0.522764791743503, -0.305013142666254, 1.15717123798939, 0.2520159428455, -0.605289057486165, -1.31287289581953, -0.722255850087244))
 39: list(xx = c(0.239089836774301, -0.774803787840354, -0.783404179125114, -0.832367003250169, 0.0388393668043031, -1.77868880692283, 0.215830349826847, 0.818167572395521, -1.44135784428725, 0.283954586122315))
 40: list(xx = c(-0.375480035299562, -1.20276372275419, -0.0383146591989489, 1.07382314112952, 0.0894824417912284, 1.94203341656778, -0.68378055989483, 0.0898382973241094, -0.000857423035673499, 0.0202359906978775))
 41: list(xx = c(2.01826759738758, 0.916580459212329, -0.105731817447497, -1.47631557557065, 1.83444429524634, -0.0793259476497517, 0.643604528792947, 0.469499793339115, 0.479360057789983, -0.50028790347691))
 42: list(xx = c(-0.0352239141420068, -0.251858948405901, -0.778256348310291, -0.0761513293674084, 1.26226930674998, -1.21635990310172, 0.924766834238583, -0.501186066819265, -0.348264648643492, -0.501316325962396))
 43: list(xx = c(0.132360072048979, 0.168625776020706, -0.333361105325462, 1.43884849538204, 1.75798855123001, -0.769170981823142, -0.474150318664124, -0.464608470935572, 0.313733203336965, 0.485829174619692))
 44: list(xx = c(-0.512760436119984, 2.40842281890321, -0.147137133519991, -0.75330767673044, 0.642564262085207, 1.93378577919486, 0.13483213204337, 0.825378112444569, 0.465193497565311, -0.742706489830422))
 45: list(xx = c(0.638812368778479, -0.154149393142712, 0.413994102621539, -0.895440911800722, 1.43882728035025, -0.0791805496603086, -0.287401093596665, 0.229677456565761, -0.410878418390726, -1.52900247924697))
 46: list(xx = c(0.00646265540205494, -0.65686201657495, -1.83572578190303, -3.01708399082002, 0.25434222075359, -0.685802863661391, -2.75355393523664, -0.496696805200868, -2.23745430499081, 1.78355542616263))
 47: list(xx = c(0.0535638005799504, 0.220510294656666, 1.40362434931304, 0.286991608813448, 0.928651017225359, -0.565391732531668, 0.613929521624712, 1.11026922860272, 0.221226966509032, -1.63740805058375))
 48: list(xx = c(1.93499808603492, 1.54608438353731, 0.671937431154049, 1.06822740130527, -1.25439898760272, -1.35505659337007, -0.328884004884974, 0.239491598021876, -1.26288447504078, 0.390738434319939))
 49: list(xx = c(1.40360651276041, -1.19227312178417, -2.33049588739773, -0.859706281554311, 1.36336452254535, -0.691317548525066, 1.55365995183534, -2.35402553290405, 0.723900307342199, -0.497643051910192))
 50: list(xx = c(-0.975865087957767, -0.0780825849834439, -0.47567206396668, 1.26685314746554, -0.490108307498817, 0.873261089990006, 1.12889537902973, 0.581650379654655, 0.0181756323963705, 0.52859959546982))
 51: list(xx = c(0.340877266204743, -0.657698759069658, 0.809252568696146, 0.15583707159419, 0.608315891279189, 0.197997215909997, 0.233870380656685, -0.449130199190705, -0.955073648866273, -0.217072852737233))
 52: list(xx = c(-0.385947004257514, -0.872375729800895, 1.97704372223215, 0.299538894061321, 0.876828646714355, 0.602437651273383, -0.62428123926471, 0.774631749643064, 1.02955841750671, 0.432498855935179))
 53: list(xx = c(-0.276024234921353, 0.743109661339846, 1.24159532114996, -0.441286744562945, -0.315485651411495, 0.872725364715408, -0.689050970954825, -0.97617782480047, -0.0563302903490221, -0.324697266226813))
 54: list(xx = c(-0.533953480795704, -2.09736928620854, 1.05577319953963, -0.865848882300705, 2.10974131894811, 0.0504375173094828, -0.00775026627905211, -0.695170876987164, -1.20634112757432, 0.797960095659388))
 55: list(xx = c(-1.93310211034414, 0.450463798665148, -0.758256284405861, -0.377060967689357, 1.12331080952397, 0.570060525324537, -0.161040423538615, -0.117262931196352, 0.866841857639318, 0.674710836228788))
 56: list(xx = c(-1.12146998891089, 0.783681102002211, 1.30990627128525, -0.17523963730412, 1.27285061460725, 0.897371047863283, -0.60556624545744, 0.304251029493958, 0.241197603603242, 0.0920878312204646))
 57: list(xx = c(1.32458652769843, -0.751104988564323, -0.500608744769052, 0.0741511015197127, 0.240205386504012, -0.127480010480759, 0.142610593726065, 0.606040778571716, 0.0402534964194334, -1.23197054589308))
 58: list(xx = c(-0.261166864396731, 0.438402475217131, -1.71070122660128, -2.09315524011744, 0.401479996626278, 0.160143892926287, -0.0384108170773027, 0.557380032600564, 0.795060060268968, 1.14866885701373))
 59: list(xx = c(0.216074110285562, -1.27648273190186, 0.466884227579527, 0.230213879752189, 0.618563962843487, 0.294501979879704, -1.15671880347238, 1.28778065589, 0.137330911989714, -2.25130837503775))
 60: list(xx = c(-0.381581211509381, 0.467089696897193, 1.35621479085206, -0.290036099150702, -1.48703109645863, -0.772182515191906, 2.07972928111516, -0.446363768654825, 0.336034880440849, -1.37652340004888))
 61: list(xx = c(-0.515312669692205, 0.132929737618521, 1.01594097790359, -0.572581322841381, 1.08980427561106, -0.907256666442157, 0.311038253581049, 0.123993837348809, -0.529544184357858, -0.147287053528977))
 62: list(xx = c(-1.30380279348406, -1.13268994367018, 0.419643207186495, -0.212154493825761, -0.354594452156044, -0.00136183068531714, 1.03273921119692, -0.378490654235764, 0.491276394803959, 0.150222595550415))
 63: list(xx = c(0.600079238307892, 0.0829429233828053, -0.00708178939083514, 1.06909855374544, -0.716948781339844, 0.334185044161647, 0.835339556991552, 0.412096045465084, -1.12953337008064, 1.3359919722217))
 64: list(xx = c(1.21872868899553, -0.942668583485807, -0.738018125915648, -0.246729622748405, -0.776844529684338, 0.793360124776663, 0.36942644791933, -1.49804153682514, 1.65252041788983, -0.0483298275985489))
 65: list(xx = c(0.632631322586066, -0.513402474428327, 2.18769721039049, -0.628765387537721, 1.27744168389688, -1.14818481490839, -0.69324162977909, 0.0670363131287407, -1.22384740999988, 1.31887590355041))
 66: list(xx = c(-0.346851531080491, 0.834763700184023, -1.22434620896666, 0.702436400614627, 1.19438335102162, 0.513329354810992, 0.911469967350954, -0.157058074747908, -1.59863776573532, -0.906242068861276))
 67: list(xx = c(0.748911880064459, 1.0988912590648, 0.444603289973282, 0.606347275798715, -0.773187811458008, -1.2418199238944, 0.00416177170846423, 0.941151872317199, 0.673682165520078, 0.370431579914044))
 68: list(xx = c(-0.787251648477367, 0.0333681411851561, 0.806820093305596, 0.961837811165717, 0.704318855249208, 0.643326195943544, -0.94338528184815, 1.51543586336394, -0.504610538880452, 0.000946408939580818))
 69: list(xx = c(-1.18366411582145, 1.07704914722774, 0.592676775726766, -0.00680032326741504, 2.90118062506634, -1.66550362505092, -1.51506574418581, -0.61195188224985, 1.43877253081114, 1.36162160574752))
 70: list(xx = c(-0.956128477472894, 0.0834270305202419, 0.0161557243608126, 0.133287842370426, 1.17271959561727, -0.443000501488192, -0.810073980915045, 0.937007551184725, 2.56059623963727, 1.04237155415089))
 71: list(xx = c(-0.11642492103393, -0.229535767832472, 0.562692282194938, 1.84600522057094, 0.94294036164923, -0.166745082582901, 0.660142626067474, -0.640129994954174, -1.21666647438951, -0.51295716048501))
 72: list(xx = c(1.08326807760006, 1.32652428936318, -0.437024091239019, -0.0612549999979144, -0.598408681263692, 1.08000887024404, -0.92504604549063, -1.11523880814544, -0.830286252842441, -0.327919397132609))
 73: list(xx = c(0.107751508523841, -0.288470661404394, 0.867780120912975, -0.744062490158716, 0.588605497200354, -1.2916436384714, 0.187045358159395, -1.44590075027514, 0.126067268679375, 0.618513208990575))
 74: list(xx = c(-0.741604932873505, 1.07723318281259, -0.712143828961614, 1.52803429547996, 0.951035789809323, -0.162674299524685, 1.14231643458741, -0.438418607756535, 2.27712270652861, -0.168036264249057))
 75: list(xx = c(1.35539725549887, 1.92612419891417, 0.00337047649186302, 1.03230649916637, -1.40808543748928, -2.02247782122625, 0.261400543971411, -0.16880447145609, 1.68975900312691, 0.514882972139111))
 76: list(xx = c(-1.27232305311825, 1.7804478127475, -0.865810217589076, 0.21066136582292, -0.793257623469713, 3.0121905596063, -0.220712836710715, -1.22499076931972, -0.963106242161662, -0.0652466185685751))
 77: list(xx = c(1.28344540046621, -0.826067788008031, 0.742040748175909, 0.255627126397904, 2.00566653584417, 0.378376565766712, 1.80267951304107, -1.41614826380702, -0.0147808607806896, 1.05300936669111))
 78: list(xx = c(1.2246191763438, 0.537972546957164, 1.01333148162609, 0.320056774221944, -0.413992538930304, -0.121790088218626, 0.248346048900635, 0.109781071921531, -1.4974055106281, 0.898741669386074))
 79: list(xx = c(0.164242735471627, 0.50163286343248, -1.27829987705834, -0.272064369474435, 1.42967184915636, 0.550436650558608, 0.579562871975906, 0.626267434860576, 1.61071033186907, 0.666238104541863))
 80: list(xx = c(-1.49174339447685, -0.537571361670517, -1.45697647041518, 1.26811084553918, -0.897745248421277, -2.20989632589644, 1.54361056746037, -0.691160955634792, 0.39221948280518, -0.0527589282358435))
 81: list(xx = c(1.92993786081509, 0.188098983586544, -0.870490390041831, -0.540836339430234, 2.81440877216255, 0.119621146223124, 1.2040447485344, 0.92486236435612, -0.26790954472333, 0.432964593186415))
 82: list(xx = c(-0.293973414690457, -0.145756017492512, 0.384553415751651, 0.550739308724548, -1.83310989237562, 1.86615683261578, -0.776596148003873, 0.722473314999104, 0.0257999505982837, 2.0811227672877))
 83: list(xx = c(0.674310471466942, 1.01240941827861, -1.52681210152973, -0.0293091257766119, 0.411501861644417, 0.811786126785395, 0.198554423131177, 0.0754945451240548, 0.646937718743459, -0.206777395968401))
 84: list(xx = c(0.523982007454175, 0.209916002144032, -0.313935874853645, 0.146036859582453, 1.0562723711803, 1.03022035124092, -0.409561629691911, 1.57767246906791, 0.652059579448694, 0.603127750107737))
 85: list(xx = c(0.390185855573965, 0.225568735655799, 0.195933142751312, 0.340173166207988, 0.217348863126386, -1.51386737715391, -0.015759942907071, 0.0884127136765867, 1.30298937315403, -0.304544892557131))
 86: list(xx = c(-0.457606266647179, 1.34489221444613, -1.02183823359941, -1.13717936316046, 1.44854603913813, 0.0463643698853324, 0.87884983689366, -1.15884978616332, 0.0736530549145434, 0.271512496255096))
 87: list(xx = c(2.18603293875314, -2.08359840544169, 0.838245923177047, -0.800171064907571, 1.79604983922653, -0.545775094052457, -0.727741067432444, 1.73600440866011, 0.661373107269451, 1.1290601674651))
 88: list(xx = c(-0.313315276508584, 0.162865817869458, 0.253508185794975, 0.445522558036042, 1.00625073023096, 0.385234131452745, 0.150807428077675, -0.751626100265879, 0.0555799662424774, 0.146968944923843))
 89: list(xx = c(0.390356263904186, -0.365855699075706, -0.725483150699569, -1.23490755240362, 0.88703681509095, -0.361235340231773, -0.403294847529562, 0.35299149985665, 1.44294615518593, -0.867058786715591))
 90: list(xx = c(-0.143629284972958, 1.72467008853103, 1.45725062273413, 1.25328332621215, -0.960441706966134, 1.04957889747073, -0.194154944400323, 0.644349900767465, 0.0382511784963183, -1.16547684903771))
 91: list(xx = c(-1.60661348848625, -0.804100985619705, 0.0557195800772942, -0.327237239528685, 0.294499901748057, 0.00973779787991136, -0.903524987540901, -0.345190412271808, 1.14579831215061, -1.71292568152095))
 92: list(xx = c(0.543852745400128, 0.209175023106355, -0.753446635953722, 0.386603784064606, -0.122905266727503, 1.87360405627933, -1.58052059601951, 1.0229641902385, -0.921561976023871, 0.227282736915856))
 93: list(xx = c(-0.269607621547857, 0.266265436424968, 0.016412680669008, -1.45800694871676, -1.38451035155491, 0.0613803675112054, 1.12095677012977, -0.738236975129818, -0.580116118447407, 0.516089528570432))
 94: list(xx = c(-0.0391354584708934, -0.731101172728587, -0.069910981365799, 0.0681392588435648, 0.819138979163894, 0.558842055186423, 0.676541278244785, 0.132126522545594, -0.171827938874385, 0.652247934568452))
 95: list(xx = c(-0.605015157964977, 0.941829728270339, 0.71197502711617, 1.232115882708, -0.618935027907035, 1.19383075494675, 0.706152582112534, 0.569618366054876, -0.825154618890158, -0.505589797572715))
 96: list(xx = c(0.313035772334153, -0.668872311871761, 0.275326893433315, -0.428055071334419, 1.74462372528644, -0.511086989202529, -1.00424892314565, 0.544708274629562, -1.09748127362634, 0.523064370459928))
 97: list(xx = c(0.799857066210345, -0.553426645337281, 0.443929660251424, -0.29103286237111, 0.49533195799919, 0.0276037338158214, -0.648539503413302, 0.763473737494046, -0.742531905426067, 0.10583937526957))
 98: list(xx = c(0.570206490571214, 1.50757922709502, -0.950591711105384, -0.516808012521562, 1.78384128157516, 0.197545681191048, -0.0451844462218608, 1.18810495451021, -0.294742769804136, 0.0879317286690119))
 99: list(xx = c(-0.491123693385345, 1.01434386388443, -0.605738290036756, 0.022312631203674, 0.608523954594457, 0.17704220796078, -1.54287386429479, 0.996208988670401, 0.82559027438757, -0.202645562545544))
100: list(xx = c(-1.68420975083113, 1.74500855516562, -0.800960164292082, -0.289903662652963, -0.576649827453154, 1.57631102912814, 1.18333037886034, -0.133706587155603, 0.555987899212748, 0.241748168164959))

Selection: 

#5

type in a number into the console after “Select:” appears to choose the row you want. Then look at the value of x. For example

Selection: 3
> x
[[1]]
            xx
1   1.14864280
2  -1.13780185
3  -2.44854667
4  -1.12119936
5   0.81304187
6   0.93139808
7   0.05886257
8   0.88755960
9   1.18696491
10  1.90916593

#6

If you think you’ve found a bug, please file an issue on GitHub.
Thanks


#7

on which project? rstudio | purrr?


#8

purrr. If it’s an issue with a package, file it in the package repo.


#9

modify_depth should do the trick :

> library(tidyverse)
> y <- tibble(a=purrr::rerun(10,tibble(x=purrr::rerun(100,data.frame(xx=rnorm(10))))))
> 
> modify_depth(y, 3, "xx")
# A tibble: 10 x 1
                    a
               <list>
 1 <tibble [100 x 0]>
 2 <tibble [100 x 0]>
 3 <tibble [100 x 0]>
 4 <tibble [100 x 0]>
 5 <tibble [100 x 0]>
 6 <tibble [100 x 0]>
 7 <tibble [100 x 0]>
 8 <tibble [100 x 0]>
 9 <tibble [100 x 0]>
10 <tibble [100 x 0]>

Or maybe map2_dfc :

> map2_dfc(
+   c(3,2), 
+   c("xx","x"), 
+   ~ modify_depth(y, .x, .y)
+ )
# A tibble: 10 x 2
                    a           a1
               <list>       <list>
 1 <tibble [100 x 0]> <list [100]>
 2 <tibble [100 x 0]> <list [100]>
 3 <tibble [100 x 0]> <list [100]>
 4 <tibble [100 x 0]> <list [100]>
 5 <tibble [100 x 0]> <list [100]>
 6 <tibble [100 x 0]> <list [100]>
 7 <tibble [100 x 0]> <list [100]>
 8 <tibble [100 x 0]> <list [100]>
 9 <tibble [100 x 0]> <list [100]>
10 <tibble [100 x 0]> <list [100]>

#10

you are returning empty tbl_df’s

yy <- modify_depth(y, 3, "xx")
yy$a[[1]]
# A tibble: 100 x 0

#11

how can map2_df be changed to be generic, in this case?


#12

In your access fonction my_element, do you really want to use argument x in two places or is it a typo ? I ask because you also have an x element in your list.

It seems to me that you want somethink like this

my_element <- function(arg) arg[[2]]$x ?

Then, where does the function select comes from, dplyr ?
Because with arg[[2]]$x you get a list of data.frame, not a data.frame. I think you need to bind them or use a map call before doing a select.

I try to clarify the question to better understand your example and what you want to do.
I get something like this right now, but not sure it is what you mean and don’t see what you mean by concatenate id from each parent level

library(purrr)
library(tibble)
y <- tibble(a=purrr::rerun(10,tibble(x=purrr::rerun(100,data.frame(xx=rnorm(10))))))

pluck(y, "a", function(arg) arg[[2]]$x %>% dplyr::bind_rows() %>% dplyr::select("xx")) %>% str(1)
#> 'data.frame':    1000 obs. of  1 variable:
#>  $ xx: num  -0.126 0.983 -0.93 0.828 1.443 ...

Created on 2018-01-12 by the reprex package (v0.1.1.9000).


#13

Thanks for the answer.

In this case i would want a way to traverse back in Id column ie

pluck(y, "a", function(arg) arg[[2]]$x %>% dplyr::bind_rows(.id = 'myId'))%>%head(.,20)
   myId         xx
1     1  1.5569967
2     1  0.3944150
3     1 -1.6962801
4     1 -1.2061633
5     1  0.3048078
6     1  0.8720869
7     1  0.7571711
8     1 -1.0019825
9     1  1.6301896
10    1 -0.1783199
11    2 -1.2478445
12    2 -0.3902659
13    2 -0.8293694
14    2 -0.4663907
15    2 -1.2366525
16    2  0.8491159
17    2 -1.1252083
18    2  1.2829380
19    2 -0.5792747
20    2  0.7535965

but this is a toy example. in reality the depth is 6+ deep, is there a recursive solution to this? kind of what @colin was getting at with a depth param.


#14

I’m not sure what you mean by generalized purrr but a similar syntax does work in purr and is meant for navigating a tree.

suppressPackageStartupMessages(library(tidyverse))
t <- tibble(a=purrr::rerun(10,tibble(x=purrr::rerun(100,data.frame(xx=rnorm(10))))))
t %>%   pluck(1,3,1,2,1,7)
#> [1] -0.7794849
t %>% pluck("a", 1, "x", 1, "xx", 1)
#> [1] -0.3006946
t %>% pluck("a", 2, "x", 2, "xx", 1)
#> [1] 0.9794934

The tree you’ve built has some issues in that not every node in that tree has a name that is unique with respect to its siblings so at some levels they can only be referenced by child position.

Here is an example of a hand built 3 level tree that can be accessed by a path of names passed into pluck or a selector. Note that each node has a name that is unique with respect to its siblings even though the same names are repeated.

suppressPackageStartupMessages(library(tidyverse))
t2 <- list(a = list(a = list(a = 1, b = 2, c = 3), b = list(a = 4, b= 5, c = 6)),
                    b =  list(a = list(a = 10, b = 22, c = 30), b = list(a = 40, b= 50, c = 60)))

t2 %>% pluck("a", "b", "c")
#> [1] 6
t2 %>% pluck("a", "a", "b")
#> [1] 2

t2 %>% .[[c("a", "b", "c")]]
#> [1] 6
t2 %>% .[[c("a", "a", "b")]]
#> [1] 2

Just a gentle reminder a reprex with a more simple example and the specific results you are looking for would help everyone help out on finding a solution. Although I think everyone gets the general idea of what you are trying to do it would better to show a “gozinta/gozouta” picture. (goes into, goes out :slight_smile:) in addition to the general description of what you are looking for.

In your current it’s hard specify a something other than “how do I use pluck to extract 0.961480267016588 from tree?” because it is hard to visualize where that value is in the tree and it requires a mix of positions and names to get to it.

And then again I may have completely misunderstood what you want to do… wouldn’t be the first time for me :-):grin: