Search code examples
rdplyrdata.table

Rename mutiple variables based on a vector


I have this data frame,

set.seed(123)
dataf <- data.frame( id = seq(1:5), 
                     A_1 = rnorm(5),
                     A_2 = rnorm(5),
                     A_3 = rnorm(5),
                     A_4 = rnorm(5),
                     A_5 = rnorm(5))

> dataf 
  id         A_1        A_2        A_3        A_4        A_5
1  1 -0.56047565  1.7150650  1.2240818  1.7869131 -1.0678237
2  2 -0.23017749  0.4609162  0.3598138  0.4978505 -0.2179749
3  3  1.55870831 -1.2650612  0.4007715 -1.9666172 -1.0260044
4  4  0.07050839 -0.6868529  0.1106827  0.7013559 -0.7288912
5  5  0.12928774 -0.4456620 -0.5558411 -0.4727914 -0.6250393

And I have a vector like the following, based on which, I want to rename the above data frame variables.

sep <- c(0, 1, 1, 0, 0)

In this vector, I have the marker based on which the varibles should be renamed. If the marker says 0, no change. If the marker say 1, we add a prefix to the variable name, say, _mod. For example, the second element of sep is 1, that means I need to rename the second variable A_2 to A_2_mod. The resulting table should look like this:

  id         A_1    A_2_mod    A_3_mod        A_4        A_5
1  1 -0.56047565  1.7150650  1.2240818  1.7869131 -1.0678237
2  2 -0.23017749  0.4609162  0.3598138  0.4978505 -0.2179749
3  3  1.55870831 -1.2650612  0.4007715 -1.9666172 -1.0260044
4  4  0.07050839 -0.6868529  0.1106827  0.7013559 -0.7288912
5  5  0.12928774 -0.4456620 -0.5558411 -0.4727914 -0.6250393

I can do it easily by renaming by position. However, I am wondering how to make the renaming scalable in case, say, I have thousands of columns.


Solution

  • You can use dplyr::rename_with to help. For example

    dplyr::rename_with(dataf, 
       function(x) paste0(x, "_mod"), 
       .cols=num_range("A_", which(sep==1))
    )
    

    I used the num_range() tidyselect helper to choose just the A columns that have a 1 in the corresponding position in sep