How can I scale(x) only certain columns of a dataframe? I have a dataframe with 7 columns and I want to scale only column 3 and 6. The rest should stay as it is.
This type of analysis often needs column scaling in a data frame to provide meaningful results. Without normalizing, the vectors or columns you are using, you will usually get meaningless results. The scale () is a built-in R function whose default method centers and/or scales the columns of a numeric matrix.
The following R syntax shows how to standardize our example data using the scale function in R. As you can see in the following R code, we simply have to insert the name of our data frame (i.e. data) into the scale function: The previous output of the RStudio console shows the first six rows of our standardized data.
It shows that our example data consists of two numeric columns x1 and x2. The following R syntax shows how to standardize our example data using the scale function in R.
The most common way to select some columns of a data frame is the specification of a character vector containing the names of the columns to extract. Consider the following R code: data [ , c ("x1", "x3")] # Subset by name. data [ , c ("x1", "x3")] # Subset by name.
We can do this with lapply
. Subset the columns of interest, loop through them with lapply
, assign the output back to the subset of data. Here, we are using c
because the outpuf of scale
is a matrix
with a single column. Using c
or as.vector
, it gets converted to vector
df[c(3,6)] <- lapply(df[c(3, 6), function(x) c(scale(x)))
Or another option is mutate_at
from dplyr
library(dplyr)
df %>%
mutate_at(c(3,6), funs(c(scale(.))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With