8.3 Create new variables
The mutate() function creates new variables (or columns) in a data frame. By assigning the same object name to the result, the new variables become part of the object.
dcps <- dcps %>%
mutate(
LastEdit = '2021', # character variable with same value in each
ProfLangProp = ProfLang/100, # convert to proportion
)
dcps %>% select(2,5,8:9) %>% head()## # A tibble: 6 × 4
## SchName ProfLang LastEdit ProfLangProp
## <chr> <dbl> <chr> <dbl>
## 1 Aiton Elementary School 5.56 2021 0.0556
## 2 Amidon-Bowen Elementary School 16.3 2021 0.163
## 3 Anacostia High School 4.48 2021 0.0448
## 4 Ballou High School 2.78 2021 0.0278
## 5 Bancroft Elementary School @ Sharpe 34.3 2021 0.343
## 6 Barnard Elementary School 38.4 2021 0.384
In addition to transformations that apply the same function to every case, you can create variables that treat observations on an individual basis. Consider the if_else() option. This will specify a logical test to apply to each case, and specify replacement values for those observations that evaluate as true and those observations that evaluate as false. The syntax is: if_else(criteria,value.if.true,value.if.false).
dcps <- dcps %>%
mutate(
AbvMedMath = if_else(
ProfMath > median(ProfMath), # condition to evaluate
1, # output if condition is TRUE
0 # output if FALSE
)
)
dcps %>% select(SchCode,ProfMath,AbvMedMath) %>% head()## # A tibble: 6 × 3
## SchCode ProfMath AbvMedMath
## <dbl> <dbl> <dbl>
## 1 202 15.3 0
## 2 203 10.1 0
## 3 450 1.43 0
## 4 452 0.498 0
## 5 204 39.9 1
## 6 205 39.7 1