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 x 4
##   SchName                ProfLang AboveAvgMath LastEdit
##   <chr>                     <dbl>        <dbl> <chr>   
## 1 Aiton Elementary Scho~     5.56            0 2021    
## 2 Amidon-Bowen Elementa~    16.3             0 2021    
## 3 Anacostia High School      4.48            0 2021    
## 4 Ballou High School         2.78            0 2021    
## 5 Bancroft Elementary S~    34.3             1 2021    
## 6 Barnard Elementary Sc~    38.4             1 2021

In addition to transformations that apply the same function to every case, you can create variables that treat cases 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 cases that evaluate as true and those cases 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 x 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