I'm using the sub function to clean up a dataset. Changing free response gender information to 3 different categories (Male, Female, Rainbow). The function is working just fine except for a few instances, which are the following:
"Rainbow?"
"Male (CIS)"
"Guy (-ish) ^_^"
"Female (trans)"
"Female (cis)"
What is confusing me is that the function worked 40 times for the other values I needed to change but I can't change these ones. Thanks!!!!
I've tried the function numerous times but it won't work for those values and I don't know why.
mh_in_tech <- data.frame(
id = 1:4,
gender = c('femail', 'Femake', 'Rainbow?', 'Male (CIS)')
)
mh_in_tech$Gender_clean <- mh_in_tech$gender
# Here is some code that worked:
mh_in_tech$Gender_clean <- sub('femail', 'Female', mh_in_tech$Gender_clean)
mh_in_tech$Gender_clean <- sub('Femake', 'Female', mh_in_tech$Gender_clean)
# Code that did not work:
mh_in_tech$Gender_clean <- sub('Rainbow?', 'Rainbow', mh_in_tech$Gender_clean)
mh_in_tech$Gender_clean <- sub('Male (CIS)', 'Male', mh_in_tech$Gender_clean)
mh_in_tech
#> id gender Gender_clean
#> 1 1 femail Female
#> 2 2 Femake Female
#> 3 3 Rainbow? Rainbow?
#> 4 4 Male (CIS) Male (CIS)