I am calculating the average after centering a variable. By centering it implies subtracting the mean of the variable from its original variable. I am using dplyr package. I am able to do it for one variable via mutate() function. How can I do the same for multiple variables using mutate?
set.seed(1) # for reproducible example
train <- data.frame(X1=sample(1:100,100),
X2=1e6*sample(1:100,100),
X3=1e-6*sample(1:100,100))
library(dplyr)
train %>% mutate(center = X1-mean(X1)) %>%
summarise(round(mean(center),4))
mutate_allmean(x-mean(x))is obviously 0 (regardless ofx) and can give a different value only for floating point precision.