Skip to content

[PATCH] show vector length in summary()

7 messages · Dirk Eddelbuettel, Simon Urbanek, R. Michael Weylandt +1 more

#
(summary.default): show the vector length in addition to quantiles


diff -u -i -p -F '^(def' -b -w -B /home/sds/src/R-3.0.1/src/library/base/R/summary.R.old /home/sds/src/R-3.0.1/src/library/base/R/summary.R
--- /home/sds/src/R-3.0.1/src/library/base/R/summary.R.old	2013-03-05 18:02:33.000000000 -0500
+++ /home/sds/src/R-3.0.1/src/library/base/R/summary.R	2013-09-10 10:19:02.682946339 -0400
@@ -39,6 +39,7 @@ summary.default <-
 	qq <- stats::quantile(object)
 	qq <- signif(c(qq[1L:3L], mean(object), qq[4L:5L]), digits)
 	names(qq) <- c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.", "Max.")
+  qq <- c(qq,"Length" = length(object))
 	if(any(nas))
 	    c(qq, "NA's" = sum(nas))
 	else qq

Diff finished.  Tue Sep 10 10:19:40 2013
#
On 10 September 2013 at 10:32, Sam Steingold wrote:
| (summary.default): show the vector length in addition to quantiles
| 
| 
| diff -u -i -p -F '^(def' -b -w -B /home/sds/src/R-3.0.1/src/library/base/R/summary.R.old /home/sds/src/R-3.0.1/src/library/base/R/summary.R
| --- /home/sds/src/R-3.0.1/src/library/base/R/summary.R.old	2013-03-05 18:02:33.000000000 -0500
| +++ /home/sds/src/R-3.0.1/src/library/base/R/summary.R	2013-09-10 10:19:02.682946339 -0400
| @@ -39,6 +39,7 @@ summary.default <-
|  	qq <- stats::quantile(object)
|  	qq <- signif(c(qq[1L:3L], mean(object), qq[4L:5L]), digits)
|  	names(qq) <- c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.", "Max.")
| +  qq <- c(qq,"Length" = length(object))
|  	if(any(nas))
|  	    c(qq, "NA's" = sum(nas))
|  	else qq
| 
| Diff finished.  Tue Sep 10 10:19:40 2013

Base R functions are rarely modified; others may have expectations on
summary() returning the six values it returns.

Many alternatives are available, including describe in Hmisc which returns
the count you suggest, and a count of missingness.

R> set.seed(42)
R> describe(rnorm(100))
rnorm(100) 
      n missing  unique    Mean     .05     .10     .25     .50     .75     .90     .95 
    100       0     100 0.03251 -1.7641 -1.2117 -0.6167  0.0898  0.6616  1.3730  1.5116 

lowest : -2.993 -2.656 -2.440 -2.414 -1.781, highest:  1.513  1.576  1.895  2.018  2.287 
R> 


Dirk
#
Note that summary sometimes returns 5 values (when there no "NA's").
It is clearly wrong to rely on the details of the return value of a UI function.
Such a minor issue hardly justifies installing a whole new package.
(but thanks for the suggestion!)
#
On Sep 10, 2013, at 12:56 PM, Sam Steingold wrote:

            
.. except that summary() is not a UI function. It is used to create summary *objects*, not some UI output. Although sometimes users like to print such summary objects, that is not the task of summary(). There are quite common programmatic uses of summary() -- one prominent one that comes to my mind is in conjunction with connections.
 (Not that any of this has anything to do with the original question ... )
#
you mean, saving to files?
well, it does: what are the "common programmatic uses of summary" which
rely on its number of values?
#
Yes, I have seen this, thanks for the reminder.
In fact, I have some summary methods in my codebase.
However, adding 46 lines of summary.default to my file just to add one
line is no good.