Changing gender from integer to string


I am a complete newbie, so please pardon what I imagine is a very basic question. I have a gender variable in my dataframe that is coded as 1, 2 and 0 representing "Male", "Female" and "Blank".

I would like to try a couple of things:

  1. create a new separate gender variable where they are coded as strings to be "male", "female" etc.
  2. Add labels to the existing Gender variable so anyone else looking at the data knows how they were coded

Thank you so much.

Hi, in R usually factors are used to work with categorical variables.

an example


# generate some data
df <- tibble(gender = c(1, 0, 2, 1, 2, 1, 0))

# convert numerical variable to factor
df <- df %>% 
  mutate(gender = factor(gender,
                         levels = c(1, 2, 0),
                         labels = c("male", "female", "blank"))) 

#> [1] male   blank  female male   female male   blank 
#> Levels: male female blank

# converting to a numerical vector is still possible
#> [1] 1 3 2 1 2 1 3 is a very good introduction in how to work with factor variables. And if you are a newbie who would like to start working with R.

Thnk you so much! I just bookmarked your second link for myself as homework. Much appreciated.

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