4
votes

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   
3

3 Answers

5
votes

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
5
votes

We can use case_when

library(dplyr)

df %>% 
  mutate_at(vars(ends_with("x1")), ~case_when((. >= 3 & . <= 12) | . == 1 ~ 0,
                                               . >= 13 | . == 2 ~ 1))

#  id ax1 bx1
#1  1   0   1
#2  2   0   1
#3  3   0   0
#4  4   1   1
#5  5   1   1
2
votes

Here is another solution similar to what you where attempting. I just added the "or" operator (|) to make a simpler ifelse and removed the factor part from your code.

library(dplyr)
df1<-df %>% 
  mutate_at(vars(ends_with("x1")), function(x)
            ifelse(x >= 3 & x <= 12 | x == 1,0,
                   ifelse(x >= 13 | x == 2, 1,0)))

#  id ax1 bx1
#1  1   0   1
#2  2   0   1
#3  3   0   0
#4  4   1   1
#5  5   1   1

If there are no other possible conditions apart from the ones you mention (for example, having zeros), I think you could simplify it more by just reducing it to the following:

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