Home > Enterprise >  Add a new column for multiple dataframes using a for loop
Add a new column for multiple dataframes using a for loop

Time:07-20

I have multiple different dataframes and I want for each one separatly to take the rownames, and add it's values to a new column called symbol.

It's easy to do that separatly with df$symbol = rownames(df) but I'm sure there is some smarter wat to do it I just dont know how. I added all dataframes into a list, and started with:

 for (i in mylist) {

}

a small sample of the list:

list(structure(list(logFC = c(-7.27165840952755, -4.88122110881833, 
-6.30403520245801, -6.8999676658889, -8.13498951031712, -5.32413783113649
), AveExpr = c(-2.58650721158596, 3.61120284397814, -2.9750929146731, 
3.77018383049359, -0.810972490709042, 1.65932418607214), t = c(-9.0553181002117, 
-6.82620260804813, -8.48983929556853, -6.06314640365836, -6.57611123280085, 
-6.08421330131609), P.Value = c(6.52481248651775e-08, 2.95624449971451e-06, 
1.61686397946798e-07, 1.26952738955763e-05, 4.72484047815294e-06, 
1.21817558764285e-05), adj.P.Val = c(0.0021469243005638, 0.0324240896728687, 
0.00266006461902072, 0.0425873774609022, 0.0388665377732861, 
0.0425873774609022), B = c(3.93526797954365, 3.76986589394228, 
3.07593517182545, 2.93364582997393, 2.78712609618446, 2.48083026475185
)), row.names = c("CT55", "RSAD2", "NONHSAG055530", "SERPINA3", 
"MAGEA3", "ELOVL2"), class = "data.frame"), structure(list(logFC = c(-1.11702292814244, 
-1.00881339314944, -1.29247483340359, -1.678804336036, -0.877301841172139, 
-0.882994503750135), AveExpr = c(-1.04650550960762, 2.3264480164653, 
1.89169499858488, 1.85255219430914, 3.017518955549, 3.24731558132494
), t = c(-5.50653192580488, -5.18990981628195, -5.19737421339365, 
-5.19357612170011, -5.16155285521296, -5.00851768836733), P.Value = c(1.36714704185616e-07, 
6.07519509403496e-07, 5.86918245169044e-07, 5.97314354344639e-07, 
6.92387529999473e-07, 1.39075321882931e-06), adj.P.Val = c(0.00226891723066448, 
0.00229817268957425, 0.00229817268957425, 0.00229817268957425, 
0.00229817268957425, 0.00297000171237909), B = c(5.78292151217496, 
5.75319693459223, 5.74841163529183, 5.72216201772023, 5.67676638224385, 
5.04092739413463)), row.names = c("C4orf50", "EOMES", "FCRL3", 
"CXCL13", "SLAMF6", "TOX"), class = "data.frame"), structure(list(
    logFC = c(-0.565887124026144, 0.612912631013749, -0.618398238451896, 
    -0.667091570949291, -0.892065809446931, -1.58344446295073
    ), AveExpr = c(2.75051982430423, 4.52932455304796, 4.40916124787407, 
    3.49506443574801, -0.305786556309331, 2.75701352117834), 
    t = c(-6.45466482244236, 5.50739226476167, -5.22815126014602, 
    -5.21073977458381, -5.33075404093159, -5.1367248495367), 
    P.Value = c(4.36358632890185e-10, 7.84679183670794e-08, 3.21918341064428e-07, 
    3.50880529362477e-07, 1.92897841200425e-07, 5.048066324975e-07
    ), adj.P.Val = c(7.40587871741222e-06, 0.000443919170175357, 
    0.000951953129638619, 0.000951953129638619, 0.000818465540213404, 
    0.000951953129638619), B = c(12.3621067175863, 7.68387994397188, 
    6.37209400308848, 6.28105629010098, 6.12986402238888, 5.94569278120403
    )), row.names = c("REXO5", "PCDHGC3", "ESPL1", "SGO1", "RDM1", 
"CXCL9"), class = "data.frame"))

now what ?

CodePudding user response:

A possible solution:

library(tidyverse)

map(mylist, ~ .x %>% rownames_to_column("symbol") %>% `row.names<-`(.$symbol))

#> [[1]]
#>                      symbol     logFC    AveExpr         t      P.Value
#> CT55                   CT55 -7.271658 -2.5865072 -9.055318 6.524812e-08
#> RSAD2                 RSAD2 -4.881221  3.6112028 -6.826203 2.956244e-06
#> NONHSAG055530 NONHSAG055530 -6.304035 -2.9750929 -8.489839 1.616864e-07
#> SERPINA3           SERPINA3 -6.899968  3.7701838 -6.063146 1.269527e-05
#> MAGEA3               MAGEA3 -8.134990 -0.8109725 -6.576111 4.724840e-06
#> ELOVL2               ELOVL2 -5.324138  1.6593242 -6.084213 1.218176e-05
#>                 adj.P.Val        B
#> CT55          0.002146924 3.935268
#> RSAD2         0.032424090 3.769866
#> NONHSAG055530 0.002660065 3.075935
#> SERPINA3      0.042587377 2.933646
#> MAGEA3        0.038866538 2.787126
#> ELOVL2        0.042587377 2.480830
#> 
#> [[2]]
#>          symbol      logFC   AveExpr         t      P.Value   adj.P.Val
#> C4orf50 C4orf50 -1.1170229 -1.046506 -5.506532 1.367147e-07 0.002268917
#> EOMES     EOMES -1.0088134  2.326448 -5.189910 6.075195e-07 0.002298173
#> FCRL3     FCRL3 -1.2924748  1.891695 -5.197374 5.869182e-07 0.002298173
#> CXCL13   CXCL13 -1.6788043  1.852552 -5.193576 5.973144e-07 0.002298173
#> SLAMF6   SLAMF6 -0.8773018  3.017519 -5.161553 6.923875e-07 0.002298173
#> TOX         TOX -0.8829945  3.247316 -5.008518 1.390753e-06 0.002970002
#>                B
#> C4orf50 5.782922
#> EOMES   5.753197
#> FCRL3   5.748412
#> CXCL13  5.722162
#> SLAMF6  5.676766
#> TOX     5.040927
#> 
#> [[3]]
#>          symbol      logFC    AveExpr         t      P.Value    adj.P.Val
#> REXO5     REXO5 -0.5658871  2.7505198 -6.454665 4.363586e-10 7.405879e-06
#> PCDHGC3 PCDHGC3  0.6129126  4.5293246  5.507392 7.846792e-08 4.439192e-04
#> ESPL1     ESPL1 -0.6183982  4.4091612 -5.228151 3.219183e-07 9.519531e-04
#> SGO1       SGO1 -0.6670916  3.4950644 -5.210740 3.508805e-07 9.519531e-04
#> RDM1       RDM1 -0.8920658 -0.3057866 -5.330754 1.928978e-07 8.184655e-04
#> CXCL9     CXCL9 -1.5834445  2.7570135 -5.136725 5.048066e-07 9.519531e-04
#>                 B
#> REXO5   12.362107
#> PCDHGC3  7.683880
#> ESPL1    6.372094
#> SGO1     6.281056
#> RDM1     6.129864
#> CXCL9    5.945693

CodePudding user response:

lapply(mylist, function(x) { x$symbol <- rownames(x); x })
  • Related