mutate(acs12,
long_commute = case_when(XXXX ~ "yes",
~ "no")) XXXX
ex-6-01
Creating new variables exercise
This is supplementary information for the exercise you were given on June 1 on creating variables. I’ve provided some incomplete template code to help get you started on the exercises. If you get stuck on something, copy and paste the contents of the code chunks here into your assignment. Then determine what you need to add to replace XXXX
.
Q1
Create a version of acs12
with a new variable called long_commute
with the value “yes” if a respondent commutes more than 30 minutes to work and “no” otherwise.
hint: you need to know which variable tells you what someone’s commute time is. How would you find this out?
Edit the above code to give this new dataset a name (aka, save it as an object).
hint: this is what <-
is for
Then look at it in the data viewer (click its name in the “Environment” panel). Does it look like your new variable worked as you expected?
Q2
Create a version of acs12
with a new variable called collegeplus
. It should have the value TRUE
if someone has a college or graduate school degree and FALSE
if not.
<- mutate(acs12,
acs12_collegeplus collegeplus = XXXX)
Check your work by creating a table that shows you if your new variable maps onto the old variable in the way you expect.
table(XXXX, XXXX, useNA = "always")
Extra credit
Create a new variable that indicates whether someone works a. not at all (0 work hours), b. part time (1-30 hours), c. full time (30-50 hours), or d. too much (50+ hours). Check the data to see if it worked correctly.
<- mutate(acs12,
acs12_workcat XXXX = case_when(hrs_work == 0 ~ "not at all",
~ XXXX,
XXXX ~ XXXX,
XXXX ~ XXXX)) XXXX
Create a new variable that indicates whether someone is an English-speaking citizen, a non-English speaking citizen, an English-speaking non-citizen, or a non-English speaking non-citizen. Check the data to see if it worked correctly.
<- mutate(acs12,
acs12_citizenlanguage XXXX = XXXX)
Do the same thing as above, but this time use |>
to provide the acs12
dataset to the function
|>
acs12 mutate(XXXX)
Using pipes (|>
), create a variable that indicates whether someone is a married woman, unmarried woman, married man, or unmarried man, then remove all minors (less than 18 years old) from the dataset. Check your work using tables, summary statistics, and/or by looking at the data frame. This one is challenging!
Submit
As usual, submit your work by committing your changes and then pushing to github! Please submit before class on Tuesday 6-6.