Mutate error when attempting to recode - object not found

Viewed 566

I’m trying to change the Gender variable in my dataset to a character variable (i.e., 1 = male, 2 = female, 3 = other)

The code I attempted to run is the following

bwdata6 <- bwdata6 %>% mutate(across(c(Gender),
~recode(.x, ‘1’ = male, ‘2’ = female, ‘3’ = other)))

The error message I got is below.

Error: Problem with mutate() input ..1.
x object ‘male’ not found
i Input ..1 is across(c(Gender), ~recode(.x, 1= male,2= female,3 = other)).

What’s going wrong here? Any ideas how to solve this?

1 Answers

The value can be quoted

library(dplyr)
bwdata6 <- bwdata6 %>% 
           mutate(across(c(Gender), ~recode(.x, '1' = 'male', 
                    '2' = 'female', 
                '3' = 'other')))

As a reproducible example

v1 <- c(1, 2, 3, 1)
recode(v1, '1'= male)

Error in list2(...) : object 'male' not found

With quotes,

recode(v1, '1'= 'male', '2' = 'female', '3' = 'other')
#[1] "male"   "female" "other"  "male"  
Related