Is there a way to label the "0"'s and "1"'s of my gender variable,

111 views Asked by At

Is there a way to label the "0"'s and "1"'s of my gender variable, as 'Female' and 'male' so that on plotting i get "gender=male" instead of gender=1. I would also be ok with Male and Female. I rarely work with R, so forgive my simple question.

I tried

gender2 = factor(gender2, levels = c("Female", "male")

but it gave me an error. it also made me uncomfortable that i couldn't specify the values for the ordering so make sure female is for coding 0 and male for coding 1.

1

There are 1 answers

2
DaveArmstrong On

The levels argument of factor should be the values in your existing variable. The labels argument is where you identify the words that should correspond with the existing values (in the same order). For example:

gender2 <- factor(gender2, levels=c(0,1), labels=c("Female", "Male")) 

The above would attach "Female" to the value 0 and "Male" to the value 1.