EML
EML

Reputation: 671

Replacing values based on condition with dplyr

I am relatively new to dplyr and I am trying to replace values for all columns if a given column contains only 0's and 1's.

df <- data.frame(a=c(1,2,3,4), b=c(1,1,0,0), c=c(1,1,1,0))

Here is my attempt:

df_recode <- df %>% dplyr::mutate_if((min(., na.rm=T)==0 & max(., na.rm=T)==1), 
                                      dplyr::recode(., `1`="yes", `0`="no"))

I am getting the following error:

Error in tbl_if_vars(.tbl, .p, .env, ..., .include_group_vars = .include_group_vars) : length(.p) == length(tibble_vars) is not TRUE

Thank you in advance!

Upvotes: 1

Views: 430

Answers (1)

akrun
akrun

Reputation: 887881

We can wrap with all to return a logical vector of length 1 in mutate_if and then do the recode

df %>% 
    dplyr::mutate_if(~ all(. %in% 0:1), ~ dplyr::recode(., `1` = 'yes', `0` = 'no'))
#  a   b   c
#1 1 yes yes
#2 2 yes yes
#3 3  no yes
#4 4  no  no

In the OP's code, we need the ~ to specify the anonymous function

df %>% 
 dplyr::mutate_if(~(min(., na.rm=TRUE)==0 & max(., na.rm=TRUE)==1),  
            ~dplyr::recode(., `1`="yes", `0`="no"))
#  a   b   c
#1 1 yes yes
#2 2 yes yes
#3 3  no yes
#4 4  no  no

Upvotes: 1

Related Questions