3.8 Creating New Variables using Conditional statements

We can instead create a new variable in our main dataframe. Let’s make a variable that takes the value 1 if a name is female and black sounding and 0, otherwise

# Initialize a new variable called femaleblackname
resume$femaleblackname <- NA
# Assign a 1 to our new variable where sex is female and race is black
resume$femaleblackname[resume$sex == "female" & resume$race == "black"] <- 1
# Assign a 0 if sex is not female OR if race is not black
resume$femaleblackname[resume$sex != "female" | resume$race != "black"] <- 0

We can check our work

table(name = resume$firstname, femaleblack = resume$femaleblackname)
##           femaleblack
## name         0   1
##   Aisha      0 180
##   Allison  232   0
##   Anne     242   0
##   Brad      63   0
##   Brendan   65   0
##   Brett     59   0
##   Carrie   168   0
##   Darnell   42   0
##   Ebony      0 208
##   Emily    227   0
##   Geoffrey  59   0
##   Greg      51   0
##   Hakim     55   0
##   Jamal     61   0
##   Jay       67   0
##   Jermaine  52   0
##   Jill     203   0
##   Kareem    64   0
##   Keisha     0 183
##   Kenya      0 196
##   Kristen  213   0
##   Lakisha    0 200
##   Latonya    0 230
##   Latoya     0 226
##   Laurie   195   0
##   Leroy     64   0
##   Matthew   67   0
##   Meredith 187   0
##   Neil      76   0
##   Rasheed   67   0
##   Sarah    193   0
##   Tamika     0 256
##   Tanisha    0 207
##   Todd      68   0
##   Tremayne  69   0
##   Tyrone    75   0

Let’s say we wanted to know the callback rates for just female black (sounding) names.

mean(femaleblack$call)
## [1] 0.06627784
mean(resume$call[resume$femaleblackname == 1])
## [1] 0.06627784

BINGO: two ways to do the same thing.

3.8.1 ifelse statements

Remember how we created the variable femaleblack, well there is another way to do that in R using what are called conditional statements with ifelse().

  • Can be read: If this relational statement is TRUE, I assign you A, otherwise I assign you B
resume$femaleblackname <- ifelse(resume$sex == "female" &
                                   resume$race == "black", 1, 0)

Can be read: If sex is female and race is black, give the observation in the new variable a 1, otherwise give it a 0.

Like most things, we can also get more complicated here. Let’s say we wanted to create a variable that indicated both race and sex.

  • Can be read: If this relational statement is TRUE, I assign you A,
  • Otherwise if this second relational statement is TRUE, I assign you B,
  • Otherwise if this third relational statement is TRUE, I assign you C,
  • Otherwise I assign you D
resume$racesex <- ifelse(resume$sex == "female" &
                                   resume$race == "black", "FemaleBlack", 
                         ifelse(resume$sex == "female" &
                                   resume$race == "white", "FemaleWhite",
                                ifelse(resume$sex == "male" &
                                   resume$race == "white", "MaleWhite", "MaleBlack")))

Note: what you assign can be numeric or text.