Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

merging lists of lists together and creating a new column based on the information contained in one of the lists

$
0
0

I am trying to create a new column in one of 2 separate lists.

One element of df1 looks like:

$`c(5, 19)`
$`c(5, 19)`[[1]]
               Feature           Gain         Cover    Frequency
 1:     plaza_eliptica 0.948578681145 0.53759794901 0.2794117647
 2:               wind 0.014083116347 0.10011187610 0.1343137255
 3:               temp 0.011637657812 0.08581378948 0.1460784314
 4:               year 0.006344014204 0.12430881478 0.1137254902
 5:           humidity 0.004941509318 0.03941622272 0.0862745098
 6:          barometer 0.003729098869 0.03750491037 0.0715686275
 7:             season 0.003482740507 0.02015837244 0.0254901961
 8:              month 0.003016223359 0.03462645560 0.0539215686
 9:                day 0.002926824939 0.00525381171 0.0578431373
10:            weekday 0.000644114655 0.01335391670 0.0176470588
11:      week_of_month 0.000587970927 0.00074890364 0.0117647059
12: workday_on_holiday 0.000025880557 0.00107281595 0.0009803922
13:            holiday 0.000002167362 0.00003216151 0.0009803922

The first element of df2 looks like

[[1]]
[[1]][[1]]
     date         c_farolillo
[1,] "2016-01-01""17"       

[[1]][[2]]
     date         c_farolillo
[1,] "2016-01-02""9"        

[[1]][[3]]
     date         c_farolillo
[1,] "2016-01-03""8"        

[[1]][[4]]
     date         c_farolillo
[1,] "2016-01-04""3"        

[[1]][[5]]
     date         c_farolillo
[1,] "2016-01-05""4"        

[[1]][[6]]
     date         c_farolillo
[1,] "2016-01-06""4"

I am trying to take the first element of df2 and create a new column in df1. That is, I want to take this element:

[[1]]
[[1]][[1]]
     date         c_farolillo
[1,] "2016-01-01""17"

and add a column to element 1 of df1 such as:

$`c(5, 19)`
$`c(5, 19)`[[1]]
               Feature           Gain         Cover    Frequency   date       c_farolillo
 1:     plaza_eliptica 0.948578681145 0.53759794901 0.2794117647  2016-01-01      17
 2:               wind 0.014083116347 0.10011187610 0.1343137255  2016-01-01      17
 3:               temp 0.011637657812 0.08581378948 0.1460784314  2016-01-01      17
 4:               year 0.006344014204 0.12430881478 0.1137254902  2016-01-01      17
 5:           humidity 0.004941509318 0.03941622272 0.0862745098  2016-01-01      17
 6:          barometer 0.003729098869 0.03750491037 0.0715686275  2016-01-01      17
 7:             season 0.003482740507 0.02015837244 0.0254901961  2016-01-01      17
 8:              month 0.003016223359 0.03462645560 0.0539215686  2016-01-01      17
 9:                day 0.002926824939 0.00525381171 0.0578431373  2016-01-01      17
10:            weekday 0.000644114655 0.01335391670 0.0176470588  2016-01-01      17
11:      week_of_month 0.000587970927 0.00074890364 0.0117647059  2016-01-01      17
12: workday_on_holiday 0.000025880557 0.00107281595 0.0009803922  2016-01-01      17
13:            holiday 0.000002167362 0.00003216151 0.0009803922  2016-01-01      17

Then take element 2 of df2:

[[1]][[2]]
     date         c_farolillo
[1,] "2016-01-02""9"

and do the same thing but for element 2 of df1.

$`c(5, 19)`[[2]]
               Feature          Gain        Cover   Frequency   date       c_farolillo
 1:     plaza_eliptica 0.95025739085 0.5490795291 0.283433134  2016-01-02       9
 2:               temp 0.01236820897 0.0832973356 0.150698603  2016-01-02       9
 3:               wind 0.01196041617 0.0895609496 0.125748503  2016-01-02       9
 4:               year 0.00604315510 0.1158975396 0.112774451  2016-01-02       9
 5:             season 0.00511480982 0.0173938219 0.027944112  2016-01-02       9
 6:           humidity 0.00500999458 0.0578155014 0.086826347  2016-01-02       9
 7:          barometer 0.00325812831 0.0340156062 0.071856287  2016-01-02       9
 8:              month 0.00323898173 0.0354103288 0.062874251  2016-01-02       9
 9:                day 0.00220665600 0.0067323511 0.058882236  2016-01-02       9
10:            weekday 0.00050300478 0.0103857430 0.014970060  2016-01-02       9
11: workday_on_holiday 0.00001964502 0.0002228799 0.000998004  2016-01-02       9
12:      week_of_month 0.00001960867 0.0001884139 0.002994012  2016-01-02       9

Such that all 3 lists in each of the 2 main lists contain the merged data. That is, list3, element 3 of df1 would have merged to gether:

[[2]][[3]]
     date         pza_del_carmen
[1,] "2016-01-03""10" 

$`c(7, 1, 2, 18)`[[3]]
               Feature          Gain        Cover    Frequency   date        pza_del_carmen
 1:      pza_de_espana 0.75620312440 0.2776437590 0.1729106628  2016-01-03      10
 2:             retiro 0.21115176179 0.2195341962 0.1498559078  2016-01-03      10
 3:   escuelas_aguirre 0.01304161322 0.0993235815 0.0970220941  2016-01-03      10
 4:               wind 0.00497255534 0.0963420148 0.1123919308  2016-01-03      10
 5:               temp 0.00490558802 0.1068475040 0.1585014409  2016-01-03      10
 6:          barometer 0.00356537931 0.0580338186 0.0787704131  2016-01-03      10
 7:           humidity 0.00201778550 0.0233865914 0.0672430355  2016-01-03      10
 8:               year 0.00177749645 0.0517034409 0.0547550432  2016-01-03      10
 9:                day 0.00086333491 0.0048338563 0.0509125841  2016-01-03      10
10:              month 0.00047874430 0.0234141348 0.0211335255  2016-01-03      10
11:            weekday 0.00040798584 0.0168542292 0.0144092219  2016-01-03      10
12:      week_of_month 0.00032152928 0.0032202756 0.0105667627  2016-01-03      10
13:             season 0.00026657228 0.0186674991 0.0105667627  2016-01-03      10
14: weekend_on_holiday 0.00002652936 0.0001950987 0.0009606148  2016-01-03      10

Data1:

list1 <- list(`c(5, 19)` = list(structure(list(Feature = c("plaza_eliptica", 
"wind", "temp", "year", "humidity", "barometer", "season", "month", 
"day", "weekday", "week_of_month", "workday_on_holiday", "holiday"
), Gain = c(0.948578681144529, 0.0140831163472628, 0.0116376578118342, 
0.00634401420383024, 0.0049415093180091, 0.00372909886882749, 
0.00348274050673969, 0.00301622335931412, 0.00292682493887959, 
0.000644114654618996, 0.000587970926885777, 0.0000258805573006903, 
0.00000216736196828243), Cover = c(0.537597949014824, 0.100111876095501, 
0.0858137894753769, 0.12430881477959, 0.0394162227230228, 0.0375049103727748, 
0.0201583724440218, 0.034626455595298, 0.005253811712761, 0.0133539166971052, 
0.000748903637236591, 0.00107281594659352, 0.000032161505893596
), Frequency = c(0.279411764705882, 0.134313725490196, 0.146078431372549, 
0.113725490196078, 0.0862745098039216, 0.0715686274509804, 0.0254901960784314, 
0.053921568627451, 0.057843137254902, 0.0176470588235294, 0.0117647058823529, 
0.000980392156862745, 0.000980392156862745)), row.names = c(NA, 
-13L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x560f12912cd0>), 
    structure(list(Feature = c("plaza_eliptica", "temp", "wind", 
    "year", "season", "humidity", "barometer", "month", "day", 
    "weekday", "workday_on_holiday", "week_of_month"), Gain = c(0.950257390847805, 
    0.0123682089682263, 0.0119604161685161, 0.00604315510198456, 
    0.00511480981946408, 0.00500999457778123, 0.00325812831159771, 
    0.00323898173138714, 0.00220665599964529, 0.000503004779421417, 
    0.0000196450237473069, 0.0000196086704235932), Cover = c(0.549079529057103, 
    0.0832973355514094, 0.0895609496061689, 0.115897539589901, 
    0.0173938218615296, 0.0578155014108067, 0.0340156061873294, 
    0.0354103287593173, 0.00673235113002399, 0.0103857430401735, 
    0.000222879883826733, 0.000188413922410228), Frequency = c(0.283433133732535, 
    0.150698602794411, 0.125748502994012, 0.112774451097804, 
    0.0279441117764471, 0.0868263473053892, 0.0718562874251497, 
    0.062874251497006, 0.0588822355289421, 0.0149700598802395, 
    0.000998003992015968, 0.0029940119760479)), row.names = c(NA, 
    -12L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x560f12912cd0>), 
    structure(list(Feature = c("plaza_eliptica", "wind", "temp", 
    "year", "humidity", "season", "barometer", "month", "day", 
    "weekday", "week_of_month", "workday_on_holiday", "holiday"
    ), Gain = c(0.949543744302507, 0.0127124388058637, 0.0124127480264104, 
    0.0058494905916539, 0.00485548305388125, 0.00377279694948541, 
    0.00374108535234525, 0.00338676185428436, 0.00281051782299812, 
    0.000616191102085985, 0.000274388024161729, 0.000019955565907055, 
    0.00000439854841619063), Cover = c(0.542114223875487, 0.101582370822065, 
    0.0785740051880257, 0.117231459930658, 0.0448908975192598, 
    0.0310638209878387, 0.0371754899100022, 0.0319736784664766, 
    0.00306042970087287, 0.0113686232431824, 0.000719155027307212, 
    0.000213678650286169, 0.0000321666785377028), Frequency = c(0.287698412698413, 
    0.138888888888889, 0.140873015873016, 0.111111111111111, 
    0.0803571428571429, 0.0307539682539683, 0.066468253968254, 
    0.0525793650793651, 0.0615079365079365, 0.0188492063492063, 
    0.00892857142857143, 0.000992063492063492, 0.000992063492063492
    )), row.names = c(NA, -13L), class = c("data.table", "data.frame"
    ), .internal.selfref = <pointer: 0x560f12912cd0>)), `c(7, 1, 2, 18)` = list(
    structure(list(Feature = c("pza_de_espana", "retiro", "escuelas_aguirre", 
    "wind", "temp", "barometer", "humidity", "year", "day", "month", 
    "weekday", "week_of_month", "season", "weekend_on_holiday"
    ), Gain = c(0.762835844259031, 0.205459059740918, 0.0130315791677542, 
    0.0045078890564497, 0.00444974962904841, 0.00339293826829134, 
    0.00189508238873358, 0.00187978643588582, 0.00100750177875752, 
    0.000538521180289064, 0.000402209068457385, 0.000300268511436018, 
    0.0002522936065142, 0.0000472769084332351), Cover = c(0.293886140204331, 
    0.227015081557907, 0.0916711798129263, 0.0951455374927713, 
    0.102043766809557, 0.0520602895145079, 0.0284397058958519, 
    0.0521635564204478, 0.00571869176893915, 0.0177917404833809, 
    0.0133466738877007, 0.00350419034156103, 0.0168233263876777, 
    0.000390119422439669), Frequency = c(0.175908221797323, 0.154875717017208, 
    0.101338432122371, 0.111854684512428, 0.136711281070746, 
    0.0736137667304015, 0.0678776290630975, 0.0583173996175908, 
    0.0506692160611855, 0.0248565965583174, 0.0181644359464627, 
    0.011472275334608, 0.0124282982791587, 0.00191204588910134
    )), row.names = c(NA, -14L), class = c("data.table", "data.frame"
    ), .internal.selfref = <pointer: 0x560f12912cd0>), structure(list(
        Feature = c("pza_de_espana", "retiro", "escuelas_aguirre", 
        "wind", "temp", "barometer", "humidity", "year", "day", 
        "month", "weekday", "week_of_month", "season", "weekend_on_holiday"
        ), Gain = c(0.762803211914528, 0.205468329058334, 0.0130409115957786, 
        0.00452752500477332, 0.0044356364989903, 0.00339296767537418, 
        0.00189335858734865, 0.00188130563208465, 0.00100789616848372, 
        0.000544801039619805, 0.000402657864212878, 0.000301123014739554, 
        0.000252994653373448, 0.0000472812923590283), Cover = c(0.293865486823143, 
        0.227012786737776, 0.0915793870076463, 0.0953268282831992, 
        0.101871655299658, 0.0520671739749038, 0.0284167576945319, 
        0.0521819149815037, 0.00571869176893915, 0.0179064814899808, 
        0.0133374946071727, 0.00349960070129703, 0.0168256212078097, 
        0.000390119422439669), Frequency = c(0.175908221797323, 
        0.154875717017208, 0.10038240917782, 0.112810707456979, 
        0.135755258126195, 0.0736137667304015, 0.0678776290630975, 
        0.0583173996175908, 0.0506692160611855, 0.0258126195028681, 
        0.0181644359464627, 0.011472275334608, 0.0124282982791587, 
        0.00191204588910134)), row.names = c(NA, -14L), class = c("data.table", 
    "data.frame"), .internal.selfref = <pointer: 0x560f12912cd0>), 
    structure(list(Feature = c("pza_de_espana", "retiro", "escuelas_aguirre", 
    "wind", "temp", "barometer", "humidity", "year", "day", "month", 
    "weekday", "week_of_month", "season", "weekend_on_holiday"
    ), Gain = c(0.756203124400033, 0.21115176178877, 0.0130416132188251, 
    0.00497255534339098, 0.00490558801959272, 0.00356537931090265, 
    0.00201778549619328, 0.00177749644506717, 0.00086333491471484, 
    0.000478744300572646, 0.000407985840782556, 0.000321529280637874, 
    0.00026657227813109, 0.0000265293623860822), Cover = c(0.277643759023313, 
    0.219534196204987, 0.0993235814605774, 0.09634201484127, 
    0.106847503999523, 0.0580338186316927, 0.0233865914427431, 
    0.0517034408518237, 0.00483385627425822, 0.0234141347833372, 
    0.0168542291651843, 0.00322027557112264, 0.0186674990876268, 
    0.000195098662541286), Frequency = c(0.172910662824208, 0.14985590778098, 
    0.0970220941402498, 0.112391930835735, 0.15850144092219, 
    0.0787704130643612, 0.0672430355427474, 0.0547550432276657, 
    0.0509125840537944, 0.021133525456292, 0.0144092219020173, 
    0.010566762728146, 0.010566762728146, 0.000960614793467819
    )), row.names = c(NA, -14L), class = c("data.table", "data.frame"
    ), .internal.selfref = <pointer: 0x560f12912cd0>)), `20` = list(
    structure(list(Feature = c("wind", "temp", "barometer", "weekday", 
    "month", "humidity", "day", "year", "season", "holiday", 
    "week_of_month", "workday_on_holiday"), Gain = c(0.548823946748944, 
    0.117337262954691, 0.0880065813349379, 0.0650600523547812, 
    0.0486228565370539, 0.0462329049013065, 0.0284432424760542, 
    0.0259893719889433, 0.017903434861155, 0.00800270117092772, 
    0.00438493579323419, 0.00119270887797099), Cover = c(0.272121569272375, 
    0.103232467200234, 0.134652633117714, 0.0874584292657969, 
    0.227533530680115, 0.0370710448415744, 0.01128348499799, 
    0.0373908197200599, 0.0580254358074773, 0.00916840258743559, 
    0.00111235975587472, 0.0209498227533531), Frequency = c(0.199107142857143, 
    0.166964285714286, 0.14375, 0.0758928571428571, 0.101785714285714, 
    0.109821428571429, 0.0830357142857143, 0.0714285714285714, 
    0.0178571428571429, 0.00892857142857143, 0.0133928571428571, 
    0.00803571428571428)), row.names = c(NA, -12L), class = c("data.table", 
    "data.frame"), .internal.selfref = <pointer: 0x560f12912cd0>), 
    structure(list(Feature = c("wind", "temp", "barometer", "weekday", 
    "month", "humidity", "year", "day", "season", "holiday", 
    "week_of_month", "workday_on_holiday"), Gain = c(0.554759003933677, 
    0.119016189751422, 0.0814029526294701, 0.0680904464958787, 
    0.0493628668136545, 0.0479577160078573, 0.0243537780791039, 
    0.023099085938702, 0.0178822245712579, 0.00818876510954599, 
    0.00428828256818778, 0.00159868810124261), Cover = c(0.295419453276322, 
    0.0990114388042247, 0.120340423199211, 0.0916497642802324, 
    0.223703084457114, 0.0363949493842049, 0.0380623469648796, 
    0.00991758944560172, 0.0563077878887549, 0.0111647114716954, 
    0.00130879289551584, 0.0167196579322443), Frequency = c(0.204385964912281, 
    0.169298245614035, 0.121929824561404, 0.0815789473684211, 
    0.101754385964912, 0.117543859649123, 0.0666666666666667, 
    0.087719298245614, 0.0175438596491228, 0.00964912280701754, 
    0.0131578947368421, 0.0087719298245614)), row.names = c(NA, 
    -12L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x560f12912cd0>), 
    structure(list(Feature = c("wind", "temp", "barometer", "weekday", 
    "month", "humidity", "day", "year", "season", "holiday", 
    "week_of_month", "workday_on_holiday"), Gain = c(0.551646372359666, 
    0.122502525130021, 0.0787453754219887, 0.0681969402680406, 
    0.0510505120913198, 0.0409036474564709, 0.0285362817251917, 
    0.0267966327642344, 0.0168811259465907, 0.00871591211535715, 
    0.0046299900040935, 0.00139468471702545), Cover = c(0.260938381459852, 
    0.106187434193848, 0.137404388371068, 0.0912161776360717, 
    0.226434831068041, 0.0361502744159383, 0.0136511366050078, 
    0.0407090277476424, 0.0604331729242941, 0.00777454726509059, 
    0.00240727756092993, 0.0166933507522171), Frequency = c(0.196996466431095, 
    0.176678445229682, 0.132508833922261, 0.0777385159010601, 
    0.101590106007067, 0.100706713780919, 0.0901060070671378, 
    0.0742049469964664, 0.019434628975265, 0.0088339222614841, 
    0.0132508833922261, 0.00795053003533569)), row.names = c(NA, 
    -12L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x560f12912cd0>))) 

Data2:

    list2 <- list(list(structure(c("2016-01-01", "17"), .Dim = 1:2, .Dimnames = list(
    NULL, c("date", "c_farolillo"))), structure(c("2016-01-02", 
"9"), .Dim = 1:2, .Dimnames = list(NULL, c("date", "c_farolillo"
))), structure(c("2016-01-03", "8"), .Dim = 1:2, .Dimnames = list(
    NULL, c("date", "c_farolillo")))), list(structure(c("2016-01-01", 
"12"), .Dim = 1:2, .Dimnames = list(NULL, c("date", "pza_del_carmen"
))), structure(c("2016-01-02", "10"), .Dim = 1:2, .Dimnames = list(
    NULL, c("date", "pza_del_carmen"))), structure(c("2016-01-03", 
"10"), .Dim = 1:2, .Dimnames = list(NULL, c("date", "pza_del_carmen"
)))), list(structure(c("2016-01-01", "11"), .Dim = 1:2, .Dimnames = list(
    NULL, c("date", "tres_olivos"))), structure(c("2016-01-02", 
"7"), .Dim = 1:2, .Dimnames = list(NULL, c("date", "tres_olivos"
))), structure(c("2016-01-03", "3"), .Dim = 1:2, .Dimnames = list(
    NULL, c("date", "tres_olivos")))))

Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>