Changing gender from integer to string

Hi,

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

library(tidyverse)

# 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"))) 

df$gender
#> [1] male   blank  female male   female male   blank 
#> Levels: male female blank

# converting to a numerical vector is still possible
as.integer(df$gender)
#> [1] 1 3 2 1 2 1 3

https://r4ds.had.co.nz/factors.html is a very good introduction in how to work with factor variables. And https://r4ds.had.co.nz/ if you are a newbie who would like to start working with R.

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

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