I want to standardize a number of columns in a dataframe, but not all columns. The columns to be manipulated are specified in a vector.
To illustrate, take the following simulated dataframe:
set.seed(1)
mydf <- data.frame(matrix(sample(100, 36, replace = TRUE), nrow = 12))
Defining the two columns to be manipulated (note that the solution should apply to a subset of columns defined by their names, not their dataframe number):
variables <- c("X1", "X2")
Now I wrote the following loop to standardize the two columns, which throws me an error.
for (i in seq_along(variables)) {
mydf[variables[i]] <- ((mydf[variables[i]] - mean(mydf[variables[i]], na.rm = TRUE)) / sd(mydf[variables[i]], na.rm = TRUE))
}
What is the correct way to do this? (I am a beginner to R.)