Search code examples
rdataframedplyrrecode

How to automate recoding of many variables using mutate_at and nested ifelse statement?


There is a large data set consisting of repeated measures of the same variable on each subject. An example data is as below

df<-data.frame(
"id"=c(1:5),
"ax1"=c(1,6,8,15,17),
"bx1"=c(2,16,8,15,17))

where "x1" is measured repeatedly so we can have "ax1", "bx1", "cx1" and so on. I am trying to recode these variables. The plan is to recode 1 and any number on the range from 3 to 12 (inclusively) as 0 and recode 2 or any value greater than or equal to 13 as 1. Because it involves many variables I am making use of "mutate_at" to automate the recoding. Also, the numbers to take on the same code are not consecutive (e.g. 1 and 3-12 to be recoded as 0) so I used a nested "ifelse" statement. I tried the following

df1<-df %>% 
mutate_at(vars(ends_with("x1")),factor, 
        ifelse(x1>=3 & x1 <=12,0,ifelse(x1==1, 0,
               ifelse(x1==2, 1,0))))

However, this fails to work because R cannot recognize "x1". Any help on this is greatly appreciated in advance. The expected output would look like

> df1
   id ax1 bx1
1  1   0   1
2  2   0   1
3  3   0   0
4  4   1   1
5  5   1   1   

Solution

  • Using ifelse, we can proceed as follows:

    df %>% 
       mutate_at(vars(ends_with("x1")),~ifelse(. ==1 | . %in% 3:12,0,
                                               ifelse(. ==2 | .>=13,1,.)))
      id ax1 bx1
    1  1   0   1
    2  2   0   1
    3  3   0   0
    4  4   1   1
    5  5   1   1