Home > Software design >  Adding a list of list as a column to a list of dataframes
Adding a list of list as a column to a list of dataframes

Time:06-16

I have a list of dataframes t_b_name and a list of list t. Now I want to add each list of t as a column to t_b_name while conserving the list format. So the t_b_name would have an additional column in each list, named, let's say, value. How can I do this? Here's the input.

t_b_name <- 
list(structure(list(ticker = c("PGN", "NRUC", "FON-CapCorp", 
"CUM", "WY"), shortname = c("Progress Engy Inc", "Natl Rural Utils Coop Fin Corp", 
"Sprint Cap Corp", "Cummins Inc", "Weyerhaeuser Co"), redcode = c("7B7589", 
"69AB75", "8D7266", "2F4123", "9F837C"), sector = c("Utilities", 
"Financials", "Telecommunications Services", "Industrials", "Basic Materials"
), region = c("N.Amer", "N.Amer", "N.Amer", "N.Amer", "N.Amer"
)), row.names = c(55529L, 54997L, 44861L, 40620L, 63060L), class = "data.frame"), 
    structure(list(ticker = c("GLW", "DPL", "ETN", "CBE", "R"
    ), shortname = c("Corning Inc", "DPL Inc", "Eaton Corp", 
    "Cooper Inds Ltd", "Ryder Sys Inc"), redcode = c("23AC85", 
    "2F65BC", "29F85D", "GI659A", "7FB89D"), sector = c("Technology", 
    "Utilities", "Industrials", "Industrials", "Industrials"), 
        region = c("N.Amer", "N.Amer", "N.Amer", "N.Amer", "N.Amer"
        )), row.names = c(20935L, 41188L, 44529L, 39663L, 55918L
    ), class = "data.frame"), structure(list(ticker = c("PHHCO", 
    "CIN", "MCCC-MedcomLLC", "FON-CapCorp", "AXL-Inc"), shortname = c("PHH Corp", 
    "CINergy Corp", "Mediacom LLC", "Sprint Cap Corp", "Amern Axle & Mfg Inc"
    ), redcode = c("6FC652", "1I96BB", "UZDA8D", "8D7266", "UU2679"
    ), sector = c("Financials", "Utilities", "Consumer Services", 
    "Telecommunications Services", "Consumer Goods"), region = c("N.Amer", 
    "N.Amer", "N.Amer", "N.Amer", "N.Amer")), row.names = c(55564L, 
    22422L, 51792L, 44861L, 38727L), class = "data.frame"))

t <- list(c(X171 = -4.84310809694633, X161 = -3.36603783246705, X112 = -2.72493266607287, 
X87 = -2.58575358943995, X210 = -2.01054909486918), c(X20 = -2.30989551860346, 
X98 = -2.19943312429299, X104 = -1.61663388964235, X72 = -1.31631234805403, 
X176 = -1.24859298915501), c(X172 = -2.67718752034399, X28 = -2.27605041741808, 
X151 = -2.25609160045945, X112 = -2.04810033395225, X57 = -1.61378307188531
))

CodePudding user response:

You could use Map() cbind (or Map() data.frame):

Map(cbind, t_b_name, value = t)

which is equivalent to

mapply(cbind, t_b_name, value = t, SIMPLIFY = FALSE)
Output
[[1]]
           ticker                      shortname redcode                      sector region     value
55529         PGN              Progress Engy Inc  7B7589                   Utilities N.Amer -4.843108
54997        NRUC Natl Rural Utils Coop Fin Corp  69AB75                  Financials N.Amer -3.366038
44861 FON-CapCorp                Sprint Cap Corp  8D7266 Telecommunications Services N.Amer -2.724933
40620         CUM                    Cummins Inc  2F4123                 Industrials N.Amer -2.585754
63060          WY                Weyerhaeuser Co  9F837C             Basic Materials N.Amer -2.010549

[[2]]
      ticker       shortname redcode      sector region     value
20935    GLW     Corning Inc  23AC85  Technology N.Amer -2.309896
41188    DPL         DPL Inc  2F65BC   Utilities N.Amer -2.199433
44529    ETN      Eaton Corp  29F85D Industrials N.Amer -1.616634
39663    CBE Cooper Inds Ltd  GI659A Industrials N.Amer -1.316312
55918      R   Ryder Sys Inc  7FB89D Industrials N.Amer -1.248593

[[3]]
              ticker            shortname redcode                      sector region     value
55564          PHHCO             PHH Corp  6FC652                  Financials N.Amer -2.677188
22422            CIN         CINergy Corp  1I96BB                   Utilities N.Amer -2.276050
51792 MCCC-MedcomLLC         Mediacom LLC  UZDA8D           Consumer Services N.Amer -2.256092
44861    FON-CapCorp      Sprint Cap Corp  8D7266 Telecommunications Services N.Amer -2.048100
38727        AXL-Inc Amern Axle & Mfg Inc  UU2679              Consumer Goods N.Amer -1.613783

CodePudding user response:

You may try

mapply(function(x,value) {cbind(x,value)}, t_b_name, t, SIMPLIFY = F)

[[1]]
           ticker                      shortname redcode                      sector
55529         PGN              Progress Engy Inc  7B7589                   Utilities
54997        NRUC Natl Rural Utils Coop Fin Corp  69AB75                  Financials
44861 FON-CapCorp                Sprint Cap Corp  8D7266 Telecommunications Services
40620         CUM                    Cummins Inc  2F4123                 Industrials
63060          WY                Weyerhaeuser Co  9F837C             Basic Materials
      region     value
55529 N.Amer -4.843108
54997 N.Amer -3.366038
44861 N.Amer -2.724933
40620 N.Amer -2.585754
63060 N.Amer -2.010549

[[2]]
      ticker       shortname redcode      sector region     value
20935    GLW     Corning Inc  23AC85  Technology N.Amer -2.309896
41188    DPL         DPL Inc  2F65BC   Utilities N.Amer -2.199433
44529    ETN      Eaton Corp  29F85D Industrials N.Amer -1.616634
39663    CBE Cooper Inds Ltd  GI659A Industrials N.Amer -1.316312
55918      R   Ryder Sys Inc  7FB89D Industrials N.Amer -1.248593

[[3]]
              ticker            shortname redcode                      sector region
55564          PHHCO             PHH Corp  6FC652                  Financials N.Amer
22422            CIN         CINergy Corp  1I96BB                   Utilities N.Amer
51792 MCCC-MedcomLLC         Mediacom LLC  UZDA8D           Consumer Services N.Amer
44861    FON-CapCorp      Sprint Cap Corp  8D7266 Telecommunications Services N.Amer
38727        AXL-Inc Amern Axle & Mfg Inc  UU2679              Consumer Goods N.Amer
          value
55564 -2.677188
22422 -2.276050
51792 -2.256092
44861 -2.048100
38727 -1.613783
  • Related