0

i'm trying to transform variable with mutation function. I'm using mutate with case_when statement.

The example is as follows. The data is about customer's preference(pref). In case of pref is 1 and 5, I will transform it to 'extreme', in case of 2 and 4, it will be 'modest', else(pref==3), it will be 'none'.

set.seed(9999)
pref<-sample(x=1:5,size=10,replace=TRUE)
df<-data.frame(pref)
df

   pref
1    5
2    1
3    1
4    4
5    3
6    5
7    5
8    1
9    5
10   1

I use the statement as follows. However, the NA appeared. It might seem that case_when only recognize the logical statement, not vector. However, using logical vector, the code become a little bit messy(The original data scale is broader than 5 scale, and condition is more complex.) How can I solve this problem?

I would appreciate all your help.

df<-df%>%mutate(prefcat=case_when(pref==c(1,5)~"extreme",
                                          pref==c(2,4)~"modest",
                                          pref==c(3)~"none"))
df
   pref prefcat
1    5  <NA>
2    1  <NA>
3    1 extreme
4    4  modest
5    3  none
6    5 extreme
7    5  <NA>
8    1  <NA>
9    5  <NA>
10   1  <NA>
ESKim
  • 422
  • 4
  • 14

2 Answers2

4

When there are multiple values to compare use %in% instead of ==. Also it is good practice to have a default TRUE argument with some default value when none of the condition matches in case_when.

library(dplyr)
df %>%
  mutate(prefcat = case_when(pref %in% c(1,5)~"extreme",
                             pref %in% c(2,4)~"modest",
                             pref == 3~"none", 
                             TRUE ~ NA_character_))

#   pref prefcat
#1     5 extreme
#2     1 extreme
#3     1 extreme
#4     4  modest
#5     3    none
#6     5 extreme
#7     5 extreme
#8     1 extreme
#9     5 extreme
#10    1 extreme
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

We can just do this in base R using a named vector

df$prefcat <- setNames(rep(c('extreme', 'modest', 'none'), 
       c(2, 2, 1)), c(1, 5, 2, 4, 3))[as.character(df$pref)]
df
#   pref prefcat
#1     5 extreme
#2     1 extreme
#3     1 extreme
#4     4  modest
#5     3    none
#6     5 extreme
#7     5 extreme
#8     1 extreme
#9     5 extreme
#10    1 extreme
akrun
  • 874,273
  • 37
  • 540
  • 662