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(.))))