[R] summary() vs mean()
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Thu Feb 1 09:04:28 CET 2001
On Wed, 31 Jan 2001, Marc R. Feldesman wrote:
> Forgive what may seem to be a trivial question/problem.
> Below is some simple R 1.2.1(Windows) code with output.
> > summary(mammals, digits=10)
> Name Body.Weight Brain.Weight
> Red Fox :1 Min. : 3.0 Min. : 26.0
> Pig :1 1st Qu.: 35.5 1st Qu.: 138.5
> Man :1 Median : 100.0 Median : 406.0
> Kangaroo:1 Mean : 761.2 Mean :1000.0
> Jaguar :1 3rd Qu.: 493.0 3rd Qu.: 667.5
> Horse :1 Max. :6654.0 Max. :5712.0
> (Other) :9
> > mean(mammals[,3])
>  1000.467 # <---summary() reports it as 1000.0
> > mean(mammals[,2])
>  761.2 # <- summary() reports it as 761.2
> I'm puzzled why the Brain.Weight mean from summary() is different from
> mean(mammals[,3]), while the Body.Weight means are identical in the two
> functions. This isn't limited to R; I've observed the same thing in S-Plus
> 2000 (and v.6 beta).
The results are to a certain number of significant figures, not decimal
> I can get the "right" answer in S-Plus using the digits argument (setting
> digits=8), but this argument doesn't seem to have any effect in R 1.2.1. I
> *did* use it the way it is illustrated in the help file as well (e.g.
> summary(mammals, digits=max(10, getOption("digits")))
> with the same results as above.
> So, I guess I have two questions:
> 1) Why does S (in both S-Plus and R 1.2.1) produce different values for
> the means in the second variable but not the first?
summary.default uses signif on the results, to by default 4 digits.
> 2) Why does the digits argument seem not to have any effect in R 1.2.1's
Because R forgot to pass it down to summary.default.
> P.S. I also pasted the example code from the summary help file into the R
> 1.2.1 window. The digits argument doesn't change the results there either.
In R and summary.data.frame, digits is only used in formatting the result.
z <- lapply(as.list(object), summary, maxsum = maxsum)
z <- lapply(as.list(object), summary, maxsum = maxsum, digits = digits)
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help