Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`mean` not producing right answer

Tags:

r

mean

As above, anyone knows what is happening?

m1 <- 0.457842055395572
m2 <- 0.587127416942505
sum(m1, m2)/2
mean(m1, m2)

Output:

> m1 <- 0.457842055395572
> m2 <- 0.587127416942505
> sum(m1, m2)/2
[1] 0.5224847
> mean(m1, m2)
[1] 0.4578421

Restarted R and still the same. Am on R 3.0 x86 Windows.

like image 853
R J Avatar asked Mar 24 '23 23:03

R J


1 Answers

mean is an S3 function that takes a single main argument which is a single R object (most commonly a vector) of quantities, and any other arguments get passed on to other methods through .... In this they get passed onto mean.default which has two extra named arguments, trim and na.rm. Therefore your second value will be interpreted as the trim amount (though you can't trim a single value).

What you want to type is:

mean(c(m1, m2))
[1] 0.5224847
like image 74
James Avatar answered Apr 20 '23 01:04

James