Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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   
like image 725
T Richard Avatar asked Dec 13 '22 09:12

T Richard


2 Answers

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
like image 131
NelsonGon Avatar answered May 17 '23 07:05

NelsonGon


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
like image 35
Ronak Shah Avatar answered May 17 '23 09:05

Ronak Shah