Search code examples
rdataframerecode

How to recode multiple variables for a subset of a dataframe?


I'm lost, so any directions would be helpful. Let's say I have a dataframe:

df <- data.frame(
  id = 1:12,
  v1 = rep(c(1:4), 3),
  v2 = rep(c(1:3), 4),
  v3 = rep(c(1:6), 2),
  v4 = rep(c(1:2), 6))

My goal would be to recode 2=4 and 4=2 for variables v3 and v4 but only for the first 4 cases (id < 5). I'm looking for a solution that works for up to twenty variables. I know how to do basic recoding but I don't see a simple way to implement the subset condition while manipulating multiple variables.


Solution

  • Here is a base R solution,

    df[1:5, c('v3', 'v4')] <- lapply(df[1:5, c('v3', 'v4')], function(i) 
                                           ifelse(i == 2, 4, ifelse(i == 4, 2, i)))
    

    which gives,

       id v1 v2 v3 v4
    1   1  1  1  1  1
    2   2  2  2  4  4
    3   3  3  3  3  1
    4   4  4  1  2  4
    5   5  1  2  5  1
    6   6  2  3  6  2
    7   7  3  1  1  1
    8   8  4  2  2  2
    9   9  1  3  3  1
    10 10  2  1  4  2
    11 11  3  2  5  1
    12 12  4  3  6  2