Recoding strings

I have these open-ended responses to the question: "What is your gender identity?"
Can anyone suggest a neat way to recode these responses to M, F, Non_binary?
Thank you!



Use case_when from tidyverse. Something like

df <- df |> mutate(new_id = case_when(gender_id %in% c("Male", "male", "he") ~"M",
gender_id %in% c("Female", "cis female","F") ~ "F"),
# etc)

You will need to be sure you have a list of all offered responses.

1 Like

Thank you, it works!
One issue is that this changes the values as I need, BUT assigns NA to anything else that is not specified. How can I keep the other values that I don't want to change?

Make a last category in the case_when

TRUE ~ gender_id

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.