[R] Use of results in summary
Mark Myatt
mark at myatt.demon.co.uk
Thu Feb 28 13:30:11 CET 2002
Patrick Buetzberger <patrick at giub.unibe.ch> writes:
>When making a summary with a vector, the result is "numeric" and I can
>recall its components (like median, mean, etc.) for further use.
>However, if I use summary with a matrix, the result is of mode
>"character" (like "Median : 1.2127") and I cannot extract the results
>for direct further use.
>Thanks for any hints,
That is annoying ... but you can take advantage of the output format
(i.e. label separated from value by a colon) to extract what you want
using strsplit() and unlist():
> my.matrix <-matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3)
> my.matrix
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> x <- summary(my.matrix)
> x
X1 X2 X3
Min. :1.00 Min. :3.00 Min. :5.00
1st Qu.:1.25 1st Qu.:3.25 1st Qu.:5.25
Median :1.50 Median :3.50 Median :5.50
Mean :1.50 Mean :3.50 Mean :5.50
3rd Qu.:1.75 3rd Qu.:3.75 3rd Qu.:5.75
Max. :2.00 Max. :4.00 Max. :6.00
> x[1,3]
[1] "Min. :5.00 "
> as.numeric(unlist(strsplit(x[1,3], ":"))[2])
[1] 5
BUT ... you could always apply() to extract just the summary you want:
> apply(my.matrix, 2, median)
[1] 1.5 3.5 5.5
And do away with summary() and the as.numeric(unlist(strsplit())[])
business altogether ... a better solution, I think.
Hope that helps,
Mark
--
Mark Myatt
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
mailing list