mutate()1. Create a new treatment variable
Review the data (d3)
# A tibble: 5 x 2
id cert
<dbl> <chr>
1 10 elem ed
2 11 special ed
3 12 elem ed
4 13 special ed
5 14 secondary ed
I have preassigned teachers to treatment in another database and I know which teachers are in the treatment condition of my study and which are in the control condition. The teachers in treatment are IDs 10, 12, and 13. I want to assign treatment teachers a 1 and control teachers a 0.
I will first create a vector of the IDs that will be assigned to
treatment. I could also do this within the variable creation function
but for clarity I am pulling out the IDs and assigning those to a vector
called treat.
Next I can create my new variable int by using the
dplyr::mutate() function.
Then to assign 1s to treatment teachers and 0s to control teachers I
chose to use dplyr::case_when().
Now I can use my vector treat and say when an
id is in my treat vector, assign it a value of
1, otherwise (which is denoted by the word TRUE) assign it a
0.
treat <- c(10, 12, 13)
d3 %>%
mutate(int = case_when(
id %in% treat ~ 1,
TRUE ~ 0
))
# A tibble: 5 x 3
id cert int
<dbl> <chr> <dbl>
1 10 elem ed 1
2 11 special ed 0
3 12 elem ed 1
4 13 special ed 1
5 14 secondary ed 0
Return to Create New Variables