1
votes

I have a large csv file that I have been trying to edit. I have been trying to create a new column titled race. There is currently a column called derived_race that has multiple races in it including White, Black or African American, Asian, joint, etc. I need to get the derived_race category down to White, Black, and Other. I have tried to use mutate to add another column, and the code that I used looked like this:

mutate(selected_public_2018_lar,Race = if (derived_race == 'White'){
print("White")
 } else if (derived_race == 'Black or African American'){
print("Black")
}else {
print("Other")
})

I keep getting an error that looks like this:

Error: Problem with mutate() input ..1. x Input ..1 can't be recycled to size 2316993. ℹ Input ..1 is selected_public_2018_lar. ℹ Input ..1 must be size 2316993 or 1, not 179666.

Thanks in advance for any help!

1

1 Answers

0
votes

If we want to create a column in a data.frame, we can either use ifelse or case_when as if/else is not vectorized and also the print is just for printing the values

library(dplyr)
selected_public_2018_lar <- selected_public_2018_lar %>%
       mutate(Race = case_when(derived_race == 'Black or African American' ~ 'Black',
          derived_race == 'White' ~ White', TRUE ~ "Other"))

Or another option is ifelse

selected_public_2018_lar %>%
    mutate(Race = ifelse(derived_race == 'Black or African American', 'Black', ifelse(derived_race == 'White', 'White', 'Other')))

Or with recode

selected_public_2018_lar %>%
    mutate(Race =  recode(`Black or African American` = 'Black',
                      'White' = 'White', .default = 'Other'))