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.

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.
Formula

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.

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.2042 0.5666 0.4697 0.6899 0.9571 3
# this function Summarize(d$y,digits=3)
#> n nvalid mean sd min Q1 median Q3 #> 102.000 99.000 0.470 0.287 0.000 0.204 0.567 0.690 #> max percZero #> 0.957 2.020
Summarize(~y,data=d,digits=3)
#> n nvalid mean sd min Q1 median Q3 #> 102.000 99.000 0.470 0.287 0.000 0.204 0.567 0.690 #> max percZero #> 0.957 2.020
Summarize(y~1,data=d,digits=3)
#> n nvalid mean sd min Q1 median Q3 #> 102.000 99.000 0.470 0.287 0.000 0.204 0.567 0.690 #> max percZero #> 0.957 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 7.912 0.785 7.000 7.000 8.000 9.000 9.000
Summarize(~v,data=d,digits=3)
#> n mean sd min Q1 median Q3 max #> 102.000 0.882 0.800 0.000 0.000 1.000 2.000 2.000 #> percZero #> 38.235
# 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 7.912 0.785 7.000 7.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 26 24 0.469 0.281 0.002 0.285 0.493 0.728 0.919 0.000 #> 2 B 23 23 0.475 0.319 0.000 0.214 0.575 0.693 0.948 4.348 #> 3 C 24 24 0.461 0.302 0.009 0.193 0.558 0.668 0.957 0.000
Summarize(y~g2,data=d,digits=3)
#> g2 n nvalid mean sd min Q1 median Q3 max percZero #> 1 female 37 36 0.406 0.267 0.002 0.198 0.339 0.608 0.957 0.00 #> 2 male 33 32 0.533 0.312 0.000 0.247 0.618 0.769 0.934 6.25 #> 3 UNKNOWN 32 31 0.479 0.276 0.021 0.308 0.586 0.690 0.948 0.00
Summarize(y~g2,data=d,digits=3,exclude="UNKNOWN")
#> g2 n nvalid mean sd min Q1 median Q3 max percZero #> 1 female 37 36 0.406 0.267 0.002 0.198 0.339 0.608 0.957 0.00 #> 2 male 33 32 0.533 0.312 0.000 0.247 0.618 0.769 0.934 6.25
## 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 female 8 8 0.372 0.232 0.002 0.236 0.409 0.560 0.627 0.000 #> 2 B female 7 7 0.366 0.262 0.012 0.214 0.295 0.528 0.775 0.000 #> 3 C female 11 11 0.438 0.311 0.076 0.205 0.303 0.622 0.957 0.000 #> 4 A male 9 8 0.570 0.306 0.061 0.344 0.651 0.783 0.919 0.000 #> 5 B male 7 7 0.653 0.320 0.000 0.601 0.684 0.874 0.934 14.286 #> 6 C male 6 6 0.466 0.368 0.009 0.189 0.458 0.797 0.862 0.000 #> 7 A UNKNOWN 9 8 0.464 0.297 0.026 0.266 0.516 0.729 0.778 0.000 #> 8 B UNKNOWN 9 9 0.420 0.332 0.021 0.085 0.445 0.642 0.948 0.000 #> 9 C UNKNOWN 7 7 0.493 0.274 0.052 0.358 0.607 0.642 0.790 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 8 8 0.372 0.232 0.002 0.236 0.409 0.560 0.627 0.000 #> 2 B female 7 7 0.366 0.262 0.012 0.214 0.295 0.528 0.775 0.000 #> 3 C female 11 11 0.438 0.311 0.076 0.205 0.303 0.622 0.957 0.000 #> 4 A male 9 8 0.570 0.306 0.061 0.344 0.651 0.783 0.919 0.000 #> 5 B male 7 7 0.653 0.320 0.000 0.601 0.684 0.874 0.934 14.286 #> 6 C male 6 6 0.466 0.368 0.009 0.189 0.458 0.797 0.862 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 36 36 0.438 0.293 0.012 0.149 0.470 0.644 0.957 0.000 #> 2 8 39 38 0.488 0.282 0.000 0.238 0.581 0.683 0.934 2.632 #> 3 9 27 25 0.487 0.294 0.000 0.201 0.606 0.727 0.948 4.000
## 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.4697 0.2871 0.0000 0.2042 0.5666 0.6899 #> max percZero #> 0.9571 2.0202 #> #> $w #> n mean sd min Q1 median Q3 max #> 102.0000 7.9118 0.7848 7.0000 7.0000 8.0000 9.0000 9.0000 #> #> $v #> n mean sd min Q1 median Q3 max #> 102.0000 0.8824 0.7997 0.0000 0.0000 1.0000 2.0000 2.0000 #> percZero #> 38.2353 #>