Skip to contents

Summary statistics for a single numeric variable, possibly separated by the levels of a factor variable or variables. This function is very similar to summary for a numeric variable.

Usage

Summarize(object, ...)

# S3 method for default
Summarize(
  object,
  digits = getOption("digits"),
  na.rm = TRUE,
  exclude = NULL,
  nvalid = c("different", "always", "never"),
  percZero = c("different", "always", "never"),
  ...
)

# S3 method for formula
Summarize(
  object,
  data = NULL,
  digits = getOption("digits"),
  na.rm = TRUE,
  exclude = NULL,
  nvalid = c("different", "always", "never"),
  percZero = c("different", "always", "never"),
  ...
)

Arguments

object

A vector of numeric data.

...

Not implemented.

digits

A single numeric that indicates the number of decimals to round the numeric summaries.

na.rm

A logical that indicates whether numeric missing values (NA) should be removed (=TRUE, default) or not.

exclude

A string that contains the level that should be excluded from a factor variable.

nvalid

A string that indicates how the “validn” result will be handled. If "always" then “validn” will always be shown and if "never" then “validn” will never be shown. However, if "different" (DEFAULT), then “validn” will only be shown if it differs from “n” (or if at least one group differs from “n” when summarized by multiple groups).

percZero

A string that indicates how the “percZero” result will be handled. If "always" then “percZero” will always be shown and if "never" then “percZero” will never be shown. However, if "different" (DEFAULT), then “percZero” will only be shown if it is greater than zero (or if at least one group is greater than zero when summarized by multiple groups).

data

A data.frame that contains the variables in formula.

Value

A named vector or data frame (when a quantitative variable is separated by one or two factor variables) of summary statistics for numeric data.

Details

This function is primarily used with formulas of the following types (where quant and factor generically represent quantitative/numeric and factor variables, respectively):

FormulaDescription of Summary
~quantNumerical summaries (see below) of quant.
quant~factorSummaries of quant separated by levels in factor.
quant~factor1*factor2Summaries of quant separated by the combined levels in factor1 and factor2.

Numerical summaries include all results from summary (min, Q1, mean, median, Q3, and max) and the sample size, valid sample size (sample size minus number of NAs), and standard deviation (i.e., sd). NA values are removed from the calculations with na.rm=TRUE (the DEFAULT). The number of digits in the returned results are controlled with digits=.

Note

Students often need to examine basic statistics of a quantitative variable separated for different levels of a categorical variable. These results may be obtained with tapply, by, or aggregate (or with functions in other packages), but the use of these functions is not obvious to newbie students or return results in a format that is not obvious to newbie students. Thus, the formula method to Summarize allows newbie students to use a common notation (i.e., formula) to easily compute summary statistics for a quantitative variable separated by the levels of a factor.

See also

See summary for related one dimensional functionality. See tapply, summaryBy in doBy, describe in psych, describe in prettyR, and basicStats in fBasics for similar “by” functionality.

Author

Derek H. Ogle, DerekOgle51@gmail.com

Examples

## Create a data.frame of "data"
n <- 102
d <- data.frame(y=c(0,0,NA,NA,NA,runif(n-5)),
                w=sample(7:9,n,replace=TRUE),
                v=sample(0:2,n,replace=TRUE),
                g1=factor(sample(c("A","B","C",NA),n,replace=TRUE)),
                g2=factor(sample(c("male","female","UNKNOWN"),n,replace=TRUE)),
                g3=sample(c("a","b","c","d"),n,replace=TRUE),
                stringsAsFactors=FALSE)

# typical output of summary() for a numeric variable
summary(d$y)   
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#>  0.0000  0.1934  0.4811  0.4845  0.7576  0.9925       3 

# this function           
Summarize(d$y,digits=3)
#>        n   nvalid     mean       sd      min       Q1   median       Q3 
#>  102.000   99.000    0.485    0.304    0.000    0.193    0.481    0.758 
#>      max percZero 
#>    0.993    2.020 
Summarize(~y,data=d,digits=3)
#>        n   nvalid     mean       sd      min       Q1   median       Q3 
#>  102.000   99.000    0.485    0.304    0.000    0.193    0.481    0.758 
#>      max percZero 
#>    0.993    2.020 
Summarize(y~1,data=d,digits=3)
#>        n   nvalid     mean       sd      min       Q1   median       Q3 
#>  102.000   99.000    0.485    0.304    0.000    0.193    0.481    0.758 
#>      max percZero 
#>    0.993    2.020 

# note that nvalid is not shown if there are no NAs and
#   percZero is not shown if there are no zeros
Summarize(~w,data=d,digits=3)
#>       n    mean      sd     min      Q1  median      Q3     max 
#> 102.000   8.039   0.702   7.000   8.000   8.000   9.000   9.000 
Summarize(~v,data=d,digits=3)
#>        n     mean       sd      min       Q1   median       Q3      max 
#>  102.000    1.137    0.797    0.000    0.250    1.000    2.000    2.000 
#> percZero 
#>   25.490 

# note that the nvalid and percZero results can be forced to be shown
Summarize(~w,data=d,digits=3,nvalid="always",percZero="always")
#>        n   nvalid     mean       sd      min       Q1   median       Q3 
#>  102.000  102.000    8.039    0.702    7.000    8.000    8.000    9.000 
#>      max percZero 
#>    9.000    0.000 

## Numeric vector by levels of a factor variable
Summarize(y~g1,data=d,digits=3)
#>   g1  n nvalid  mean    sd   min    Q1 median    Q3   max percZero
#> 1  A 23     22 0.492 0.298 0.000 0.301  0.445 0.694 0.950    4.545
#> 2  B 19     19 0.400 0.296 0.000 0.143  0.353 0.666 0.885    5.263
#> 3  C 24     24 0.560 0.287 0.058 0.287  0.588 0.778 0.953    0.000
Summarize(y~g2,data=d,digits=3)
#>        g2  n nvalid  mean    sd   min    Q1 median    Q3   max percZero
#> 1 UNKNOWN 29     29 0.484 0.313 0.002 0.175  0.436 0.750 0.950    0.000
#> 2  female 33     31 0.550 0.326 0.000 0.265  0.546 0.818 0.993    3.226
#> 3    male 40     39 0.433 0.275 0.000 0.206  0.420 0.660 0.950    2.564
Summarize(y~g2,data=d,digits=3,exclude="UNKNOWN")
#>       g2  n nvalid  mean    sd min    Q1 median    Q3   max percZero
#> 1 female 33     31 0.550 0.326   0 0.265  0.546 0.818 0.993    3.226
#> 2   male 40     39 0.433 0.275   0 0.206  0.420 0.660 0.950    2.564

## Numeric vector by levels of two factor variables
Summarize(y~g1+g2,data=d,digits=3)
#>   g1      g2 n nvalid  mean    sd   min    Q1 median    Q3   max percZero
#> 1  A UNKNOWN 7      7 0.467 0.324 0.063 0.243  0.436 0.676 0.930    0.000
#> 2  B UNKNOWN 7      7 0.406 0.312 0.002 0.173  0.343 0.666 0.820    0.000
#> 3  C UNKNOWN 6      6 0.525 0.367 0.058 0.281  0.489 0.837 0.950    0.000
#> 4  A  female 7      7 0.557 0.332 0.000 0.415  0.466 0.835 0.931   14.286
#> 5  B  female 6      6 0.550 0.294 0.108 0.372  0.603 0.757 0.885    0.000
#> 6  C  female 9      9 0.520 0.309 0.067 0.185  0.546 0.765 0.953    0.000
#> 7  A    male 9      8 0.458 0.276 0.109 0.282  0.370 0.660 0.950    0.000
#> 8  B    male 6      6 0.244 0.237 0.000 0.034  0.234 0.404 0.567   16.667
#> 9  C    male 9      9 0.622 0.224 0.248 0.497  0.629 0.799 0.903    0.000
Summarize(y~g1+g2,data=d,digits=3,exclude="UNKNOWN")
#>   g1     g2 n nvalid  mean    sd   min    Q1 median    Q3   max percZero
#> 1  A female 7      7 0.557 0.332 0.000 0.415  0.466 0.835 0.931   14.286
#> 2  B female 6      6 0.550 0.294 0.108 0.372  0.603 0.757 0.885    0.000
#> 3  C female 9      9 0.520 0.309 0.067 0.185  0.546 0.765 0.953    0.000
#> 4  A   male 9      8 0.458 0.276 0.109 0.282  0.370 0.660 0.950    0.000
#> 5  B   male 6      6 0.244 0.237 0.000 0.034  0.234 0.404 0.567   16.667
#> 6  C   male 9      9 0.622 0.224 0.248 0.497  0.629 0.799 0.903    0.000

## What happens if RHS of formula is not a factor
Summarize(y~w,data=d,digits=3)
#>   w  n nvalid  mean    sd   min    Q1 median    Q3   max percZero
#> 1 7 23     22 0.496 0.285 0.058 0.290  0.415 0.704 0.993    0.000
#> 2 8 52     50 0.471 0.296 0.007 0.200  0.462 0.692 0.950    0.000
#> 3 9 27     27 0.500 0.341 0.000 0.173  0.514 0.833 0.950    7.407

## Summarizing multiple variables in a data.frame (must reduce to numerics)
lapply(as.list(d[,1:3]),Summarize,digits=4)
#> $y
#>        n   nvalid     mean       sd      min       Q1   median       Q3 
#> 102.0000  99.0000   0.4845   0.3036   0.0000   0.1934   0.4811   0.7576 
#>      max percZero 
#>   0.9925   2.0202 
#> 
#> $w
#>        n     mean       sd      min       Q1   median       Q3      max 
#> 102.0000   8.0392   0.7025   7.0000   8.0000   8.0000   9.0000   9.0000 
#> 
#> $v
#>        n     mean       sd      min       Q1   median       Q3      max 
#> 102.0000   1.1373   0.7965   0.0000   0.2500   1.0000   2.0000   2.0000 
#> percZero 
#>  25.4902 
#>