2

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))
3
  • You can use mutate_all Commented Apr 6, 2017 at 11:51
  • I am unable to get rid of X1 in the function. Thanks in advance! Commented Apr 6, 2017 at 11:55
  • Not sure if you are giving a fake example, but mean(x-mean(x)) is obviously 0 (regardless of x) and can give a different value only for floating point precision. Commented Apr 6, 2017 at 12:14

2 Answers 2

3

Hope this is what you are looking for : (add na.rm = TRUE is required inside mean()

library(dplyr)
train %>% summarise_all(function(x) mean(x - mean(x)))
#  X1 X2            X3
#   0  0 -3.251647e-21
Sign up to request clarification or add additional context in comments.

Comments

0
train %>% mutate_each(funs(((function(x){x-mean(x)})(.)))) %>% 
summarise_each(funs(mean(.), na.rm = TRUE)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.