This function scales the numeric variables in a data frame that are specified in a given equation. Non-numeric variables mentioned in the equation are ignored. Scaling is performed by centering and dividing by the standard deviation.

scale_variables_in_equation(mydf, myeq, variables_to_exclude)

Arguments

mydf

A data frame containing the variables to be scaled.

myeq

An equation in the form of a string or an object of class formula specifying the variables to be scaled. Only the variables on the right-hand side of the equation are considered.

variables_to_exclude

vector of strings not to scale

Value

A data frame with the specified numeric variables scaled. The function modifies the input data frame in place and returns it.

Examples

data(iris)
scaled_iris <- scale_variables_in_equation(iris, myeq = "Sepal.Length ~ Sepal.Width + Petal.Length")
head(scaled_iris)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1  1.01560199    -1.335752         0.2  setosa
#> 2          4.9 -0.13153881    -1.335752         0.2  setosa
#> 3          4.7  0.32731751    -1.392399         0.2  setosa
#> 4          4.6  0.09788935    -1.279104         0.2  setosa
#> 5          5.0  1.24503015    -1.335752         0.2  setosa
#> 6          5.4  1.93331463    -1.165809         0.4  setosa