Dear R Community, 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, Patrick -------------- next part -------------- A non-text attachment was scrubbed... Name: patrick.vcf Type: text/x-vcard Size: 339 bytes Desc: Card for Patrick Buetzberger Url : https://stat.ethz.ch/pipermail/r-help/attachments/20020228/580bca94/patrick.vcf
Dear R Community, 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, Patrick -------------- next part -------------- A non-text attachment was scrubbed... Name: patrick.vcf Type: text/x-vcard Size: 339 bytes Desc: Card for Patrick Buetzberger Url : https://stat.ethz.ch/pipermail/r-help/attachments/20020228/d64bced1/patrick.vcf
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
At 12:24 28/02/02 +0100, you wrote:>Dear R Community, > >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, > >Patrick > >Attachment Converted: "C:\data\_exchange\patrick1.vcf" >Here is a suggestion. Note that it displays a warning since it uses NAs, this display can be avoided by switching off the appropriate option (`options("warn" = -1)'). my.summary.matrix <- function(M) { if (!is.matrix(M)) stop("object \"M\" is not a matrix") S <- summary(M) V <- unlist(strsplit(S, ":")) V2 <- as.numeric(V) Q <- V2[!is.na(V2)] dim(Q) <- dim(S) rownames(Q) <- V[is.na(V2)][1:6] colnames(Q) <- colnames(S) return(Q) } This function returns a numeric matrix with the summaries (min, 1st Q., ...) as rownames, and the names of the variables as colnames. (I have not tested it extensively.) Emmanuel Paradis -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._