映射列表列表并在列表data.frame中创建新列

时间:2019-12-19 21:01:13

标签: r

我有两个列表,我想使用map(., map(., ...)(或其他方法)进行映射,并将两个列表合并在一起。

也就是说,我想在shap_score中创建一个新列,该列由列表BIAS0组成。 BIAS0只有一个观察值,但是我试图在shap_score的新列中重复观察。

shap_score列表之一的预期输出为:

     holiday         temp        wind   ...  BIAS0
1 -0.2875051 -0.004131595 -0.68942320        0.0023
2 -0.2613214  0.075470507  0.15884757        0.0023
3 -0.2788497  0.148605376  0.84752327   ...  0.0023
4 -0.2852835 -0.011284750  0.11122661        0.0023
5 -0.2827380  0.012600235 -0.08916214        0.0023
6 -0.2799886  0.055347286 -0.29320240        0.0023

哪个代表2016-01-01

    lst <- list(`5` = list(`2016-01-01` = list(shap_score = structure(list(
    holiday = c(-0.287505149841309, -0.261321395635605, -0.27884966135025, 
    -0.285283505916595, -0.282738029956818, -0.279988586902618
    ), temp = c(-0.00413159467279911, 0.075470507144928, 0.14860537648201, 
    -0.0112847499549389, 0.0126002347096801, 0.0553472861647606
    ), wind = c(-0.689423203468323, 0.158847570419312, 0.847523272037506, 
    0.111226610839367, -0.089162141084671, -0.29320240020752), 
    humidity = c(0.0878895819187164, 0.115549802780151, 0.0808247774839401, 
    0.13438692688942, 0.168190106749535, 0.0706237256526947), 
    barometer = c(-0.0442701764404774, -0.0273909661918879, -0.110819362103939, 
    -0.0135176228359342, 0.017435971647501, -0.00947459880262613
    ), weekday = c(0.081529937684536, 0.111831456422806, 0.0768596306443214, 
    0.0925759300589561, 0.0789813026785851, 0.086820051074028
    ), weekend = c(0, 0, 0, 0, 0, 0), workday_on_holiday = c(-0.00449116015806794, 
    -0.00492851622402668, -0.00990568101406097, -0.00535996304824948, 
    -0.00591197935864329, -0.00449116015806794), weekend_on_holiday = c(0, 
    0, 0, 0, 0, 0), protocol_active = c(0, 0, 0, 0, 0, 0), text_fog = c(0.0545360967516899, 
    0.070147879421711, 0.0239687878638506, 0.0693793371319771, 
    0.0703154131770134, 0.0456382371485233), text_light_rain = c(0.0205550380051136, 
    0.0347262620925903, 0.0337300077080727, 0.0276936888694763, 
    0.0274705961346626, 0.0205550380051136), text_mostly_cloudy = c(-0.0035081070382148, 
    -0.0106095243245363, -0.00892648566514254, 0.00776881678029895, 
    -0.0245751272886992, -0.0141745395958424), text_passing_clouds = c(0.0114779146388173, 
    0.0142141683027148, 0.00865811575204134, 0.0236570592969656, 
    0.0147861018776894, 0.014693571254611), text_rain = c(0.0248998813331127, 
    -0.000406149541959167, -0.032954890280962, -0.000589479866903275, 
    0.00113414553925395, -0.000406149541959167), text_scattered_clouds = c(-0.0810607820749283, 
    -0.0777786746621132, -0.172431215643883, -0.0851063579320908, 
    -0.0796751528978348, -0.0967930480837822), text_sunny = c(0.00860885996371508, 
    0.00318501936271787, 0.00632673315703869, 0.00091076479293406, 
    0.00572518166154623, 0.0106474123895168), month_1 = c(0.0764073804020882, 
    0.163439929485321, 0.0846812948584557, 0.157616153359413, 
    0.0762593746185303, 0.0853563472628593), month_2 = c(0.0455782376229763, 
    0.0182493720203638, 0.0024642909411341, 0.0399442575871944, 
    0.0477627664804459, 0.0225149858742952), month_3 = c(0.00336221815086901, 
    0.00445762183517218, -0.0292761437594891, -0.000451473635621369, 
    0.0162581335753202, 0.00577702885493636), month_4 = c(-0.0627421438694, 
    -0.173753097653389, -0.0831228122115135, -0.159142002463341, 
    -0.144872710108757, -0.132649913430214), month_5 = c(-0.0746728777885437, 
    -0.147042363882065, -0.0870080813765526, -0.154210984706879, 
    -0.0838618874549866, -0.105986662209034), month_6 = c(-0.032955776900053, 
    -0.00787918921560049, -0.00770710874348879, -0.00865088775753975, 
    -0.0083921467885375, -0.0374950207769871), month_7 = c(-0.0100117931142449, 
    -0.0129543459042907, -0.0147038688883185, -0.0122463470324874, 
    -0.0122463470324874, -0.0128087708726525), month_8 = c(-0.0355697833001614, 
    -0.0451076440513134, -0.0443550981581211, -0.0451076440513134, 
    -0.0451076440513134, -0.036579642444849), month_9 = c(0.0187235474586487, 
    0.0188821014016867, 0.0159602463245392, 0.0188821014016867, 
    0.0190539490431547, 0.0190539490431547), month_10 = c(0.071361169219017, 
    0.0771452113986015, 0.0575473643839359, 0.076715886592865, 
    0.0704263001680374, 0.0722210109233856), month_11 = c(0.0580858998000622, 
    0.0873404517769814, 0.057171318680048, 0.0863843932747841, 
    0.110879138112068, 0.0858354195952415), month_12 = c(0.15059332549572, 
    0.250447928905487, 0.0866759195923805, 0.179991602897644, 
    0.115858882665634, 0.179295092821121)), row.names = c(NA, 
-6L), class = "data.frame"), mean_shap_score = structure(list(
    . = c(0.360876396982335, 0.247068853404152, 0.135545302893898, 
    0.123760943490689, 0.108000884954977, 0.107575414474356)), row.names = c("wind", 
"holiday", "month_10", "month_12", "temp", "text_scattered_clouds"
), class = "data.frame"), BIAS0 = structure(list(BIAS = 0.00230497121810913), row.names = c(NA, 
-1L), class = "data.frame")), `2016-01-02` = list(shap_score = structure(list(
    holiday = c(-0.272332519292831, -0.340749680995941, -0.29936483502388, 
    -0.262564092874527, -0.244395390152931, -0.24520780146122
    ), temp = c(0.134692505002022, 0.111628539860249, 0.0722469985485077, 
    0.0883198082447052, 0.0662637650966644, 0.0605684258043766
    ), wind = c(0.143159076571465, 0.776454508304596, 0.118829421699047, 
    -0.0511538982391357, -0.242683932185173, -0.280367434024811
    ), humidity = c(0.129436731338501, 0.0423655584454536, 0.0963118970394135, 
    0.143828451633453, 0.0356308110058308, 0.125916793942451), 
    barometer = c(-0.035864919424057, -0.0873878672719002, -0.0221946015954018, 
    -0.00450283708050847, -0.00959178991615772, 0.0137610333040357
    ), weekday = c(0.114081330597401, 0.0863068029284477, 0.0976831987500191, 
    0.0876984223723412, 0.0789007022976875, 0.0907445028424263
    ), weekend = c(0, 0, 0, 0, 0, 0), workday_on_holiday = c(-0.00402905605733395, 
    -0.00453507993370295, -0.00482375361025333, -0.00508680287748575, 
    -0.00402905605733395, -0.00376600702293217), weekend_on_holiday = c(0, 
    0, 0, 0, 0, 0), protocol_active = c(0, 0, 0, 0, 0, 0), text_fog = c(0.11833580583334, 
    0.0388534627854824, 0.115720294415951, 0.120073936879635, 
    0.0912353321909904, 0.0499505363404751), text_light_rain = c(0.0520697049796581, 
    0.0487560331821442, 0.0497864708304405, 0.0607201047241688, 
    0.0531328953802586, 0.050849661231041), text_mostly_cloudy = c(-0.0203497968614101, 
    -0.0203211829066277, -0.0203497968614101, -0.0237297993153334, 
    -0.0105950376018882, -0.0152477817609906), text_passing_clouds = c(0.00878167618066072, 
    0.000530663761310279, 0.0143402041867375, 0.0136684160679579, 
    0.00791957695037127, 0.00819030683487654), text_rain = c(-0.00133128161542118, 
    -0.0482824929058552, -0.0013865182409063, 0.000755225541070104, 
    -0.0013865182409063, -0.0013865182409063), text_scattered_clouds = c(-0.0839326903223991, 
    -0.166067808866501, -0.104155018925667, -0.0906047150492668, 
    -0.109635159373283, -0.0941797941923141), text_sunny = c(0.00581001630052924, 
    0.00568450707942247, 0.00266551366075873, 0.0067223091609776, 
    0.0134993428364396, 0.0094219297170639), month_1 = c(0.109191954135895, 
    0.0550585612654686, 0.123545065522194, 0.0814228802919388, 
    0.0547124668955803, 0.0466522350907326), month_2 = c(0.00459166103973985, 
    0.00601228745654225, 0.0340671129524708, 0.0411521792411804, 
    0.015175917185843, -0.00683757895603776), month_3 = c(0.00803727563470602, 
    -0.0509188324213028, 0.00803727563470602, 0.00101890868972987, 
    -0.000487668992718682, 0.00101890868972987), month_4 = c(-0.172974124550819, 
    -0.14289490878582, -0.172974124550819, -0.174231216311455, 
    -0.179455310106277, -0.174041390419006), month_5 = c(-0.114909239113331, 
    -0.0815929844975471, -0.114909239113331, -0.0847142040729523, 
    -0.119625508785248, -0.0925337672233582), month_6 = c(-0.0256863348186016, 
    -0.0171801336109638, -0.0256863348186016, -0.0254424661397934, 
    -0.0288356766104698, -0.0256347432732582), month_7 = c(-0.00124008720740676, 
    -0.000914117030333728, -0.00219869031570852, -0.00219869031570852, 
    0.00217337417416275, 0.00217337417416275), month_8 = c(-0.0525347702205181, 
    -0.0453627146780491, -0.0501848794519901, -0.05464031919837, 
    -0.0498320944607258, -0.047506932169199), month_9 = c(0.0249587465077639, 
    0.0150943985208869, 0.026452062651515, 0.0224876832216978, 
    0.021443136036396, 0.021443136036396), month_10 = c(0.0767439901828766, 
    0.0637840330600739, 0.076332226395607, 0.0759803578257561, 
    0.0758688449859619, 0.0741193741559982), month_11 = c(0.0604153387248516, 
    0.0535821802914143, 0.047236505895853, 0.129973396658897, 
    0.0794788673520088, 0.0823308974504471), month_12 = c(0.260544240474701, 
    0.104838013648987, 0.194464340806007, 0.136765509843826, 
    0.1962860673666, 0.228263437747955)), row.names = c(NA, -6L
), class = "data.frame"), mean_shap_score = structure(list(. = c(0.34787637969349, 
0.263833619147267, 0.137870378298983, 0.126111664515006, 0.105620052684184, 
0.0995344268686366)), row.names = c("wind", "holiday", "month_12", 
"month_10", "month_5", "temp"), class = "data.frame"), BIAS0 = structure(list(
    BIAS = 0.00215145945549011), row.names = c(NA, -1L), class = "data.frame"))), 
    `6` = list(`2016-01-01` = list(shap_score = structure(list(
        holiday = c(-0.01831397973001, -0.0278525706380606, -0.0545091107487679, 
        -0.0852110907435417, -0.0915239304304123, -0.0201904978603125
        ), temp = c(0.0619977787137032, 0.240116253495216, 0.30674609541893, 
        0.0303262006491423, -0.0750823989510536, 0.0211294107139111
        ), wind = c(-0.980749845504761, 0.299531370401382, 0.994995355606079, 
        0.362696558237076, 0.219618409872055, -0.307217717170715
        ), humidity = c(0.0190069638192654, 0.0383657030761242, 
        -0.0103078661486506, 0.386975973844528, 0.0216114148497581, 
        0.0369361937046051), barometer = c(-0.0764741972088814, 
        -0.0111954780295491, -0.132382750511169, 0.0809806659817696, 
        0.282660692930222, 0.0890090838074684), weekday = c(0.0523185282945633, 
        0.0715024098753929, 0.10260059684515, 0.0199155658483505, 
        0.0760686323046684, 0.0664166510105133), weekend = c(0, 
        0, 0, 0, 0, 0), workday_on_holiday = c(-0.00574090098962188, 
        -0.0297964438796043, -0.027728658169508, -0.107685536146164, 
        -0.110589444637299, -0.0184716582298279), weekend_on_holiday = c(0, 
        0, 0, 0, 0, 0), protocol_active = c(0, 0, 0, 0, 0, 0), 
        text_fog = c(0.0996797382831573, 0.106587544083595, 0.115442782640457, 
        0.0638781487941742, 0.0548176616430283, 0.102432563900948
        ), text_light_rain = c(0.000621219805907458, 0.000409607600886375, 
        -0.0062907743267715, 0.00471668830141425, 0.0146307684481144, 
        0.00518008414655924), text_mostly_cloudy = c(-0.1487677693367, 
        -0.130784466862679, -0.150917783379555, -0.151641234755516, 
        -0.0495412796735764, -0.129829555749893), text_passing_clouds = c(0.0073356693610549, 
        0.0170528776943684, -0.00333861657418311, 0.0242675002664328, 
        0.00929313059896231, 0.00433033425360918), text_rain = c(0.00624925876036286, 
        0.0306352786719799, 0.031936164945364, 0.0310601592063904, 
        0.0337994284927845, 0.0347826145589352), text_scattered_clouds = c(-0.0313251540064812, 
        -0.00891299080103636, -0.0361328050494194, -0.0527477525174618, 
        0.00748181901872158, -0.0359514877200127), text_sunny = c(0.00310508860275149, 
        0.00278438744135201, 0.00266698561608791, 0.00256888521835208, 
        0.0030412464402616, 0.00211086357012391), month_1 = c(0.0124078718945384, 
        0.115149907767773, 0.0342674888670444, 0.0768248960375786, 
        0.0654645636677742, 0.0636573806405067), month_2 = c(0.00906078889966011, 
        0.00684765540063381, 0.00684765540063381, 0.0043728593736887, 
        0.00523211201652884, 0.00523211201652884), month_3 = c(-0.00860895030200481, 
        0.0395329669117928, -0.00739339413121343, 0.0254243556410074, 
        0.0387462340295315, 0.000762063078582287), month_4 = c(0.0156710669398308, 
        -0.0731847062706947, -0.059317272156477, -0.0797693580389023, 
        -0.063595674932003, -0.0707821249961853), month_5 = c(0.0155536076053977, 
        -0.00135426537599415, -0.000712861423380673, 0.00186582200694829, 
        0.00462800171226263, 0.00815873499959707), month_6 = c(-0.00416068313643336, 
        -0.00582700222730637, -0.00630857422947884, -0.00630857422947884, 
        -0.00371318752877414, -0.0028462684713304), month_7 = c(-0.0765896588563919, 
        -0.0602815672755241, -0.0606993623077869, -0.0679243355989456, 
        -0.0981804504990578, -0.143634766340256), month_8 = c(-0.0274758692830801, 
        -0.029958751052618, -0.0222828015685081, -0.0231222733855247, 
        -0.0426335595548153, -0.0394838415086269), month_9 = c(0.0263891816139221, 
        0.0408347435295582, 0.0264717359095812, 0.0400248542428017, 
        0.0434771627187729, 0.0341347306966782), month_10 = c(0.0503858253359795, 
        0.0485818050801754, 0.0558837354183197, 0.0611822977662086, 
        0.056214764714241, 0.0590351931750774), month_11 = c(0.0138247236609459, 
        0.0193855240941048, 0.0193508081138134, 0.0653398260474205, 
        0.0657737776637077, 0.0138247236609459), month_12 = c(0.0957958102226257, 
        0.135555163025856, 0.124385118484497, 0.165062874555588, 
        0.177980005741119, 0.212877199053764)), row.names = c(NA, 
    -6L), class = "data.frame"), mean_shap_score = structure(list(
        . = c(0.400683706247415, 0.160881078211374, 0.114046945856654, 
        0.0954511656763976, 0.0916842353508965, 0.0821915370408051
        )), row.names = c("wind", "temp", "month_12", "text_mostly_cloudy", 
    "month_10", "barometer"), class = "data.frame"), BIAS0 = structure(list(
        BIAS = 0.00201910734176636), row.names = c(NA, -1L), class = "data.frame")), 
        `2016-01-02` = list(shap_score = structure(list(holiday = c(-0.0269235819578171, 
        -0.0375330299139023, -0.150185480713844, -0.180965185165405, 
        -0.0315828360617161, -0.0701441094279289), temp = c(0.243527591228485, 
        0.371963232755661, 0.0472849868237972, -0.0628702938556671, 
        0.0950800850987434, 0.0399793609976768), wind = c(0.312333345413208, 
        1.05714356899261, 0.438581466674805, 0.249333217740059, 
        -0.289588332176208, -0.241006031632423), humidity = c(0.122070841491222, 
        0.0394690744578838, 0.30509290099144, 0.145370110869408, 
        0.127359732985497, 0.120208732783794), barometer = c(-0.00119671260472387, 
        -0.141092225909233, 0.165500268340111, 0.272881299257278, 
        0.180314168334007, 0.366155594587326), weekday = c(0.0754338949918747, 
        0.102761223912239, 0.0114814033731818, 0.0844534561038017, 
        0.0735206827521324, 0.0815589874982834), weekend = c(0, 
        0, 0, 0, 0, 0), workday_on_holiday = c(-0.00934421829879284, 
        -0.0113556934520602, -0.0479315407574177, -0.0799224302172661, 
        -0.00402316637337208, -0.00376265193335712), weekend_on_holiday = c(0, 
        0, 0, 0, 0, 0), protocol_active = c(0, 0, 0, 0, 0, 0), 
            text_fog = c(0.121573232114315, 0.142543613910675, 
            0.0842383652925491, 0.0666860789060593, 0.118839487433434, 
            0.0962422415614128), text_light_rain = c(-0.00105138635262847, 
            -0.00085716979810968, -0.00249776896089315, -0.00258254911750555, 
            -0.00207408331334591, -0.00207408331334591), text_mostly_cloudy = c(-0.102763526141644, 
            -0.145860776305199, -0.17062683403492, -0.107765935361385, 
            -0.0928198248147964, -0.0847341865301132), text_passing_clouds = c(0.00650645792484283, 
            -0.0119929825887084, 0.00129361264407635, 0.00169141637161374, 
            0.00034726966987364, 0.000147873506648466), text_rain = c(0.0432413183152676, 
            0.0620547793805599, 0.0612813010811806, 0.0676166415214539, 
            0.067610040307045, 0.0667802914977074), text_scattered_clouds = c(0.00680965557694435, 
            -0.0301695875823498, -0.0163288377225399, 0.00500796362757683, 
            -0.013679507188499, -0.0105399489402771), text_sunny = c(0.00175180716905743, 
            0.0044323937036097, 0.00130739971064031, -0.000148897292092443, 
            0.000350211892509833, 0.00125334970653057), month_1 = c(0.13130210340023, 
            0.0630867704749107, 0.140457063913345, 0.141706764698029, 
            0.0985265597701073, 0.0603985860943794), month_2 = c(-0.00919519551098347, 
            0.0138415843248367, 0.0109194563701749, 0.0105534996837378, 
            0.0103707788512111, 0.0100245578214526), month_3 = c(0.0568007789552212, 
            -0.0135478051379323, 0.000756092253141105, 0.00378916412591934, 
            -0.0378580540418625, -0.0227362904697657), month_4 = c(-0.0758921280503273, 
            -0.0510909892618656, -0.0998639911413193, -0.0716328546404839, 
            -0.0660951882600784, -0.0549110621213913), month_5 = c(0.000941433012485504, 
            0.000817757972981781, 0.0000343952524417546, 0.0000343952524417546, 
            0.000308761722408235, 0.000308761722408235), month_6 = c(-0.0190888661891222, 
            -0.0180853568017483, -0.0180853568017483, -0.0131239648908377, 
            -0.0131239648908377, -0.0192493200302124), month_7 = c(-0.0366223938763142, 
            -0.033847738057375, -0.033847738057375, -0.0919496417045593, 
            -0.0880158543586731, -0.0880158543586731), month_8 = c(0.00105724367313087, 
            0.0109531516209245, -0.00258957524783909, -0.0119656454771757, 
            -0.0273083839565516, -0.0315780155360699), month_9 = c(0.040503092110157, 
            0.0272863991558552, 0.0342562571167946, 0.0368664264678955, 
            0.0334650687873363, 0.0334650687873363), month_10 = c(0.0210105869919062, 
            0.0340395793318748, 0.0246942117810249, 0.0683412477374077, 
            0.0773100107908249, 0.0828942507505417), month_11 = c(0.0134705044329166, 
            0.0149277104064822, 0.0455087646842003, 0.0467537902295589, 
            0.0175258759409189, 0.00985155906528234), month_12 = c(0.127066493034363, 
            0.0923486724495888, 0.200634613633156, 0.169035747647285, 
            0.19908843934536, 0.209459736943245)), row.names = c(NA, 
        -6L), class = "data.frame"), mean_shap_score = structure(list(
            . = c(0.420971721993227, 0.164329549071548, 0.129385712326475, 
            0.114136796096405, 0.110441525715531, 0.09215682157331
            )), row.names = c("wind", "temp", "month_10", "humidity", 
        "holiday", "month_3"), class = "data.frame"), BIAS0 = structure(list(
            BIAS = 0.00194627046585083), row.names = c(NA, -1L
        ), class = "data.frame"))))

2 个答案:

答案 0 :(得分:2)

这是一个选项,其中我们遍历list,从嵌套组件中提取“ BIAS0”元素,并在每个list元素的“ shap_score”中创建一列,然后返回完整的list

library(purrr)
library(dplyr)
map(lst, ~ map(.x, ~ 
        {
         .x$shap_score <- mutate(.x$shap_score, BIAS0 = .x$BIAS0[[1]])
         .x
        }

      ))

答案 1 :(得分:0)

首先,我不得不说,这些嵌套列表的类型很快变得非常混乱。

以下是您列表中一个示例的选项:

tmp <- as.data.frame(lst$`6`$`2016-01-02`$shap_score)
tmp$BIAS0 <- unlist(lst$`6`$`2016-01-02`$BIAS0)