Home > Back-end >  Rename the column of a data frame within a list using a loop and dplyr
Rename the column of a data frame within a list using a loop and dplyr

Time:07-16

I have this data frame and I would like to rename the column name inside the loop, but it doesn't do what I want.


    list_prices_python[[1]]

       DATE       Instrument TR.CLOSEPRICE
       <date>     <chr>              <dbl>
     1 2009-01-01 NA                  356.
     2 2009-01-02 NA                  356.
     3 2009-01-03 NA                  356.
     4 2009-01-04 NA                  357.
     5 2009-01-05 NA                  357.
     6 2009-01-06 NA                  357.
     7 2009-01-07 NA                  357.
     8 2009-01-08 NA                  357.
     9 2009-01-09 NA                  357.
    10 2009-01-10 NA                  357.


    name_prices_python


     [1] "ASF.CN"   "CAR_p.CN" "CIC.CN"   "CLM.CN"   "CON.CN"   "CTV.CN"   "DVI_p.CN" "FGN.CN"   
 "GNO.CN"   "MYG.CN"  
    [11] "ORB.CN"   "PMG.CN"   "RPI.CN"   "TPL.CN"   "TRR.CN"  


for(i in name_prices_python){
  
  list_prices_python[[i]] %>% 
    dplyr::rename(i = `TR.CLOSEPRICE`) -> list_prices_python[[i]]
  
}

The output is each column named "i". Not what I expected.

    # A tibble: 4,944 x 3
       DATE       Instrument     i
       <date>     <chr>      <dbl>
     1 2009-01-01 NA          356.
     2 2009-01-02 NA          356.
     3 2009-01-03 NA          356.
     4 2009-01-04 NA          357.
     5 2009-01-05 NA          357.
     6 2009-01-06 NA          357.
     7 2009-01-07 NA          357.
     8 2009-01-08 NA          357.
     9 2009-01-09 NA          357.
    10 2009-01-10 NA          357.


What should I modify? Thank you.

Created on 2022-07-15 by the reprex package (v2.0.1)

CodePudding user response:

To use a variable on the LHS of an assignment in dplyr you need to both use := for the assignment and use {{}} to turn the variable into a symbol.

Here's a reproducible example:

sample_list = list(head(mtcars, 2), head(mtcars, 2))
name_vec = c("MPG1", "MPG2")
names(sample_list) = name_vec
for(i in name_vec) {
  sample_list[[i]] = sample_list[[i]] %>% rename({{i}} := mpg)
}
sample_list
# $MPG1
#               MPG1 cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4       21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag   21   6  160 110  3.9 2.875 17.02  0  1    4    4
# 
# $MPG2
#               MPG2 cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4       21   6  160 110  3.9 2.620 16.46  0  1    4    4
# Mazda RX4 Wag   21   6  160 110  3.9 2.875 17.02  0  1    4    4

Note that this assumes that the names of your list_prices_python are name_prices_python (which seems to be implied by the name and your apparently successful use of list_prices_python[[i]] inside the loop).

You can read more about this in the FAQ Use dynamic name for new column/variable in dplyr.

  • Related