Home > Back-end >  Change column name by dataframe name [R]
Change column name by dataframe name [R]

Time:03-01

Suppose you have a list (called b) that contains 3 data frames. (dot, lower, upper)

and for every data frame, you want to get a new column that is a sequence from 1 to nrow(x) where x is the data frame.

I got that part covered, the next step wants to apply the gather function. Here is my code, this makes the steps column and applies the gather function.

c <- lapply(b, function(x){
  x <- x %>% rownames_to_column(var = "steps") %>% 
    gather(key="Serie", value="value",-steps)
})

So, the thing is that I modify 3 datasets that are in the list, but I want the column called "value" to be the name of each dataset (dot, upper, lower).

I tried a loop with names(b), but didn't work, how can I accomplish this?

Data:

> dput(b)
list(dot = structure(list(IP = c(0, 2.7369172573169e-05, 0.000137209070455453, 
-0.000744026767656575, -0.00156413286030943, -0.00228196016416034, 
-0.00312226508680459, -0.0037872773527742, -0.00430587039171353, 
-0.00467669801458954, -0.00477495882543105, -0.00505847458605298, 
-0.00540344178055493, -0.00565439842200645, -0.00599008188619944, 
-0.00626867796513716, -0.0065508370919925, -0.00686214395666623, 
-0.00706675110974514, -0.0072384675650183, -0.00743220082083003, 
-0.00760104790720315, -0.00777529823499122, -0.00794391013564946, 
-0.00808652270766673, -0.00825505070316396, -0.00841983600071262, 
-0.0085585733500763, -0.00869111245510394, -0.00881036034473216, 
-0.00891989720903879, -0.00902548427469083, -0.00911459731434537, 
-0.00919566315843938, -0.00927916870842345, -0.00935610430918908, 
-0.00942755217917381, -0.0094924296033547, -0.00954805419173067, 
-0.00959897125525683, -0.00964269681989906, -0.00967723922485355, 
-0.0097066007949597, -0.00973170517810559, -0.00975273390079675, 
-0.00977046909703578, -0.00978312674005456, -0.00979128109824263, 
-0.00979571218642413, -0.00979520801051247, -0.00979011300619809
), PUNEW = c(0, 0.000347443409834787, 0.000825121156399293, 0.0011811183501046, 
0.00140135434916819, 0.00162150031053718, 0.00175382030689706, 
0.00194247105610041, 0.00215381133748019, 0.00220181452597367, 
0.00231987259163849, 0.0024769108191347, 0.00261264622971391, 
0.00275790885370148, 0.00286648223002206, 0.00293613694379145, 
0.00304100801832371, 0.00312594245593296, 0.00316152114189345, 
0.00319973414510873, 0.00322760887783203, 0.00326135711060798, 
0.00330003001432654, 0.00331938253155913, 0.00333273207362185, 
0.0033503988160052, 0.00335754733911708, 0.00335189239237215, 
0.00333626075984629, 0.00331168829306707, 0.00328870311841103, 
0.00326297934105954, 0.00323099971694541, 0.00319676458821203, 
0.00316019136754702, 0.00312024009354639, 0.00307546189845993, 
0.00302423518196703, 0.00296889468519832, 0.0029119867159572, 
0.00285255785495403, 0.002791003223503, 0.00272763337839473, 
0.00266268433365089, 0.00259664214792354, 0.00252851960601799, 
0.0024579644139139, 0.00238577245402404, 0.00231239977339578, 
0.00223808401146947, 0.00216306969865582), FYFF = c(0.56382399193111, 
1.31651708357755, 1.98783090960534, 2.54558622395606, 2.99588917120123, 
3.39089219181994, 3.78597250481704, 4.06224331441272, 4.34676143892837, 
4.71547794411855, 5.11794935802617, 5.51988194634126, 5.88603261160186, 
6.20307061775278, 6.51714082655008, 6.80547906329905, 7.05525499338592, 
7.29669671991335, 7.5407351490855, 7.78949795400025, 8.03904086857589, 
8.26932115968312, 8.48729799011533, 8.69907657712625, 8.89681152388597, 
9.08039118272944, 9.25244871925666, 9.41534084351798, 9.57462246336048, 
9.72682039234977, 9.8687950929879, 10.0033368142126, 10.1305289277091, 
10.2497286148714, 10.3607342813525, 10.4624953024191, 10.5567151393598, 
10.6447531560322, 10.7257922733285, 10.8000034038391, 10.8679534967645, 
10.9298938925012, 10.9863163427402, 11.0368376411291, 11.0812524167159, 
11.1201615720532, 11.1538163254627, 11.1823299099345, 11.2059603658702, 
11.2248865329732, 11.2395021237781)), class = "data.frame", row.names = c(NA, 
-51L)), lower = structure(list(IP = c(0, -0.000786072710268706, 
-0.000995472926938759, -0.00218080854207529, -0.00339529390580842, 
-0.00458775178429581, -0.00551903793842954, -0.00632817381932784, 
-0.00673873541125388, -0.00726758068741689, -0.00731824388739905, 
-0.00763397332005463, -0.00815618617787028, -0.00839516465810861, 
-0.00882574302883423, -0.00926881982446775, -0.00972628273444087, 
-0.0101340953792714, -0.0103293389509101, -0.0104248059144734, 
-0.0106370361617873, -0.0108442269717894, -0.011023088783927, 
-0.0112206771243802, -0.0114097310917941, -0.0116508429832777, 
-0.0118492098191019, -0.0120459156479232, -0.0122117847483766, 
-0.0123429971865773, -0.0124729003367821, -0.0126636951590088, 
-0.0128420855589456, -0.0129879215032433, -0.0131425514198123, 
-0.0133377216505074, -0.0135232676597466, -0.0136932413757047, 
-0.0138544851308095, -0.0140123048404601, -0.0141931794563814, 
-0.0143691900950379, -0.0145188242567637, -0.0146566982208145, 
-0.0147860169550477, -0.0149045837064689, -0.0150167066501145, 
-0.0151218021261379, -0.015182494803766, -0.015235988880598, 
-0.015280637621569), PUNEW = c(0, 0.000105383557378522, 0.000354677070280544, 
0.000504876900301711, 0.000590054771361749, 0.000696050212478469, 
0.000763919451078978, 0.000844345683415714, 0.000911150820457039, 
0.000968610783323295, 0.000976223576344364, 0.000942687821963658, 
0.000922542893247509, 0.000910333591179367, 0.0009077910794267, 
0.000845786759867488, 0.000828140092050293, 0.000785661225914978, 
0.000682740829190753, 0.000615719143770553, 0.000554171455916732, 
0.000483102190470576, 0.000432531652424401, 0.000398431647503377, 
0.00032238101862975, 0.000231510771503729, 0.00012698659282909, 
9.40338066127714e-06, -0.00011299587446631, -0.000256607104618876, 
-0.000410205374184983, -0.000612524421247961, -0.000816150491466581, 
-0.00101818147783333, -0.00114271615218558, -0.00125670707069997, 
-0.00136492252393467, -0.00147304024565766, -0.0015823986526504, 
-0.00166767315058324, -0.00173081402440409, -0.00179064185152104, 
-0.00187618918853673, -0.00197471002809608, -0.00208148398362073, 
-0.00218613871330493, -0.00229597759720884, -0.00244467482447795, 
-0.00259082053003928, -0.00273462210228739, -0.00287463469401117
), FYFF = c(0.404424294832196, 0.942016093638127, 1.40043196335103, 
1.7568471203656, 2.01568240196571, 2.23495288299476, 2.47209067011024, 
2.66798429747731, 2.85169414122158, 3.04676497401992, 3.22796770058625, 
3.40449111121809, 3.55796054191461, 3.68027912092588, 3.80250067473917, 
3.90677206565148, 3.99250977431767, 4.09005135502757, 4.16242125810263, 
4.24005675779562, 4.32237781107661, 4.39505104144723, 4.45827652368309, 
4.5143456456892, 4.56222488165803, 4.54430292270435, 4.50826530116451, 
4.47187252378349, 4.44053957026745, 4.38229783338292, 4.33365856648264, 
4.30739468919134, 4.28297228919237, 4.25267349970533, 4.21860042475471, 
4.18367399230506, 4.11547497904007, 4.0365086981446, 4.00123779554931, 
3.97423082900603, 3.9468995665492, 3.92003217648619, 3.89426933421986, 
3.86923395203203, 3.84433835217659, 3.81942321949322, 3.79405310479658, 
3.76728566270038, 3.73844262288921, 3.70996361028257, 3.68232316468007
)), class = "data.frame", row.names = c(NA, -51L)), upper = structure(list(
    IP = c(0, 0.000756850550593985, 0.00118003328109527, 0.00084733630112351, 
    0.000526054407341982, 8.52630236710084e-05, -0.000402736596105544, 
    -0.000826315774140899, -0.00144171601457006, -0.00198440576594942, 
    -0.00206296094306186, -0.00226628529043093, -0.00234277982271195, 
    -0.00255400188475387, -0.00280057919660645, -0.00311009829896622, 
    -0.00338984293986716, -0.00364454745899886, -0.00360763554290821, 
    -0.00351456768316717, -0.00354414429502809, -0.00354458748448941, 
    -0.00366609547073648, -0.00376950248977776, -0.00367397678175913, 
    -0.00360230614981848, -0.00361063020041266, -0.0035842607209455, 
    -0.00357004172312053, -0.00352767582132402, -0.00349712906760222, 
    -0.00350378855771592, -0.00346936321883225, -0.00341139876174162, 
    -0.00336312404606593, -0.00330783641359932, -0.00326927159678241, 
    -0.00324943781946224, -0.00326811987960384, -0.00328476610817519, 
    -0.00329758974537195, -0.00329691115367315, -0.00329693265806685, 
    -0.00329655786089206, -0.00329396082957924, -0.00329251942659367, 
    -0.00329129422318363, -0.00328838353645899, -0.00328709296792947, 
    -0.00323085326391454, -0.00315622522060676), PUNEW = c(0, 
    0.000523053205063971, 0.00112836279259834, 0.00166963745845976, 
    0.00194027542368599, 0.00234520452850679, 0.00255114908021798, 
    0.00274504054350469, 0.00302501646311212, 0.00313658150151657, 
    0.00336032350842602, 0.00365743127684583, 0.00395947414336716, 
    0.00424268841853936, 0.00445478993525852, 0.0046104965656203, 
    0.00480473258709324, 0.00500068809084292, 0.00512907482999056, 
    0.00526025507296651, 0.00540247622163363, 0.00553991671847063, 
    0.00564965591058691, 0.00572164205181171, 0.00577437633645624, 
    0.00587065338966517, 0.00597323111300449, 0.0060531251752073, 
    0.00611566149521267, 0.00616600374061519, 0.00621749205474453, 
    0.00629560472132346, 0.00640357923710773, 0.00650507763050509, 
    0.00660434037989609, 0.00670066352334895, 0.00678982681072932, 
    0.0068711135812311, 0.0069438663320797, 0.00701398852977524, 
    0.00708180177620809, 0.00714733951421228, 0.00720918583703457, 
    0.00726761392073682, 0.00732263871284672, 0.00737383931577405, 
    0.00742000200910355, 0.00748143043970002, 0.00756937750046694, 
    0.00765516683562233, 0.00773949324792934), FYFF = c(0.66939041506833, 
    1.57170845315749, 2.37986997074743, 3.10190451536891, 3.7316138687468, 
    4.3063002263185, 4.86626814773907, 5.28755820653748, 5.70359117023102, 
    6.24771542485425, 6.79520621927176, 7.32019726660066, 7.81657468609604, 
    8.2701944109952, 8.75666445351203, 9.24348580650149, 9.61302195689516, 
    9.94992880047745, 10.2984290928555, 10.6559464936142, 11.0027256210955, 
    11.3225771773743, 11.6254341077301, 11.9151783084001, 12.1842103333436, 
    12.4266432700051, 12.6496742898062, 12.8547804157369, 13.0496801294453, 
    13.2623985284774, 13.4823291476565, 13.6947380198814, 13.8978391157234, 
    14.0901056920762, 14.272714015797, 14.4440030107705, 14.6068341104009, 
    14.7631833021863, 14.9103086580716, 15.0584110398369, 15.2114433931136, 
    15.3579150854412, 15.4977920701592, 15.6280250177051, 15.7474100284283, 
    15.8580320392001, 15.9612961235576, 16.0570453534091, 16.145227392636, 
    16.2264200215447, 16.301955995616)), class = "data.frame", row.names = c(NA, 
-51L)))

CodePudding user response:

We can use map2 from purrr to pass the names of the dataframes, then use those names as the column name for the values for each dataframe. I also switched to pivot_longer, which is now preferred over gather.

library(tidyverse)

map2(.x = b, .y = names(b), ~  {
  .x %>%
    rownames_to_column(var = "steps") %>%
    pivot_longer(names_to = "Serie", values_to = {{.y}}, -steps)
})

Output

$dot
# A tibble: 153 × 3
   steps Serie        dot
   <chr> <chr>      <dbl>
 1 1     IP     0        
 2 1     PUNEW  0        
 3 1     FYFF   0.564    
 4 2     IP     0.0000274
 5 2     PUNEW  0.000347 
 6 2     FYFF   1.32     
 7 3     IP     0.000137 
 8 3     PUNEW  0.000825 
 9 3     FYFF   1.99     
10 4     IP    -0.000744 
# … with 143 more rows

$lower
# A tibble: 153 × 3
   steps Serie     lower
   <chr> <chr>     <dbl>
 1 1     IP     0       
 2 1     PUNEW  0       
 3 1     FYFF   0.404   
 4 2     IP    -0.000786
 5 2     PUNEW  0.000105
 6 2     FYFF   0.942   
 7 3     IP    -0.000995
 8 3     PUNEW  0.000355
 9 3     FYFF   1.40    
10 4     IP    -0.00218 
# … with 143 more rows

$upper
# A tibble: 153 × 3
   steps Serie    upper
   <chr> <chr>    <dbl>
 1 1     IP    0       
 2 1     PUNEW 0       
 3 1     FYFF  0.669   
 4 2     IP    0.000757
 5 2     PUNEW 0.000523
 6 2     FYFF  1.57    
 7 3     IP    0.00118 
 8 3     PUNEW 0.00113 
 9 3     FYFF  2.38    
10 4     IP    0.000847
# … with 143 more rows

Or you can still use lapply to loop over the names of each dataframe:

lapply(names(b), function(x) {
  b[[x]] %>% 
    rownames_to_column(var = "steps") %>%
    pivot_longer(names_to = "Serie", values_to = {{x}}, -steps)
})
  • Related