r - Custom Function on Multiple Variables -
i calculating average after centering variable. centering implies subtracting mean of variable original variable. using dplyr
package. able 1 variable via mutate() function. how can same multiple variables using mutate?
set.seed(1) # 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))
hope looking : (add na.rm = true
required inside mean()
library(dplyr) train %>% summarise_all(function(x) mean(x - mean(x))) # x1 x2 x3 # 0 0 -3.251647e-21
Comments
Post a Comment