standardize_variable() standardizes the selected columns in a data.frame using base::scale(). By default, this function overwrites the column to be scaled. Use the suffix argument to avoid this behavior.

standardize_variable() and standardise_variable() are synonyms.

standardize_variable(data, cols = dplyr::everything(), suffix = NULL)

standardise_variable(data, cols = dplyr::everything(), suffix = NULL)

Arguments

data

A data frame containing the variables to standardize.

cols

<tidy-select> Columns to standardize. Defaults to dplyr::everything().

suffix

A character suffix to be added to the scaled variables names. When suffix is set toNULL, the standardize_variable() function will overwrite the scaled variables. Defaults to NULL.

Value

A data frame with the standardized columns.

standardize_variable and grouped_df

Note that standardize_variable ignores grouping. Meaning that if you call this function on a grouped data frame (see dplyr::grouped_df), the overall variables' mean and standard deviation will be used for the standardization.

Examples

ho_et_al %>%
  standardize_variable(sdo)
#> # A tibble: 824 × 5
#>    id    condition              sdo linkedfate hypodescent
#>    <chr> <chr>                <dbl>      <dbl>       <dbl>
#>  1 2     Low discrimination  -0.470       6           2.33
#>  2 3     High discrimination -0.707       5.88        6   
#>  3 4     High discrimination -0.529       6.62        6   
#>  4 5     Low discrimination   1.84        5.12        5.67
#>  5 6     Low discrimination  -0.351       4.38        4   
#>  6 9     High discrimination  0.538       3.75        4   
#>  7 11    High discrimination -1.12        6.62        5.33
#>  8 12    Low discrimination   1.84        4.62        4   
#>  9 14    Low discrimination  -1.06        5.12        3.67
#> 10 16    High discrimination -1.00        6.75        4   
#> # ℹ 814 more rows

ho_et_al %>%
  standardize_variable(c(sdo, linkedfate), suffix = "scaled")
#> # A tibble: 824 × 7
#>    id    condition       sdo linkedfate hypodescent sdo_scaled linkedfate_scaled
#>    <chr> <chr>         <dbl>      <dbl>       <dbl>      <dbl>             <dbl>
#>  1 2     Low discrimi…  1.81       6           2.33     -0.470            0.636 
#>  2 3     High discrim…  1.56       5.88        6        -0.707            0.538 
#>  3 4     High discrim…  1.75       6.62        6        -0.529            1.13  
#>  4 5     Low discrimi…  4.25       5.12        5.67      1.84            -0.0525
#>  5 6     Low discrimi…  1.94       4.38        4        -0.351           -0.643 
#>  6 9     High discrim…  2.88       3.75        4         0.538           -1.13  
#>  7 11    High discrim…  1.12       6.62        5.33     -1.12             1.13  
#>  8 12    Low discrimi…  4.25       4.62        4         1.84            -0.446 
#>  9 14    Low discrimi…  1.19       5.12        3.67     -1.06            -0.0525
#> 10 16    High discrim…  1.25       6.75        4        -1.00             1.23  
#> # ℹ 814 more rows