4

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 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

2

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.