Skip to contents

statList provides a general method for calculating univariate or multivariate statistics for a matrix or data.frame stratified by one or more factors.

Usage

statList(X, factors, FUN, drop = FALSE, ...)

Arguments

X

A matrix or data frame containing the variables to be summarized

factors

A vector, matrix or data frame containing the factors for which X is to be summarized. If factors is not specified, the result is calculated for all of the data in X.

FUN

A function to be applied to the pieces of X, as split by factors.

drop

Logical, indicating whether empty levels of factors are to be dropped from the result.

...

Other arguments, passed to FUN.

Value

Returns a list of items corresponding to the unique elements in

factors, or the interaction of factors. Each item is the result of applying FUN to that collection of rows of X. The items are named according to the levels in factors.

Details

statList is the general function. X is first split by factors, and FUN is applied to the result.

colMeansList and covList are just calls to statList with the appropriate FUN.

See also

Author

Michael Friendly

Examples


# grand means
statList(iris[,1:4], FUN=colMeans)
#> [[1]]
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>     5.843333     3.057333     3.758000     1.199333 
#> 
# species means
statList(iris[,1:4], iris$Species, FUN=colMeans)
#> $setosa
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>        5.006        3.428        1.462        0.246 
#> 
#> $versicolor
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>        5.936        2.770        4.260        1.326 
#> 
#> $virginica
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>        6.588        2.974        5.552        2.026 
#> 
# same
colMeansList(iris[,1:4], iris$Species)
#> $setosa
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>        5.006        3.428        1.462        0.246 
#> 
#> $versicolor
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>        5.936        2.770        4.260        1.326 
#> 
#> $virginica
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>        6.588        2.974        5.552        2.026 
#> 

# var-cov matrices, by species
covList(iris[,1:4], iris$Species)
#> $setosa
#>              Sepal.Length Sepal.Width Petal.Length Petal.Width
#> Sepal.Length   0.12424898 0.099216327  0.016355102 0.010330612
#> Sepal.Width    0.09921633 0.143689796  0.011697959 0.009297959
#> Petal.Length   0.01635510 0.011697959  0.030159184 0.006069388
#> Petal.Width    0.01033061 0.009297959  0.006069388 0.011106122
#> 
#> $versicolor
#>              Sepal.Length Sepal.Width Petal.Length Petal.Width
#> Sepal.Length   0.26643265  0.08518367   0.18289796  0.05577959
#> Sepal.Width    0.08518367  0.09846939   0.08265306  0.04120408
#> Petal.Length   0.18289796  0.08265306   0.22081633  0.07310204
#> Petal.Width    0.05577959  0.04120408   0.07310204  0.03910612
#> 
#> $virginica
#>              Sepal.Length Sepal.Width Petal.Length Petal.Width
#> Sepal.Length   0.40434286  0.09376327   0.30328980  0.04909388
#> Sepal.Width    0.09376327  0.10400408   0.07137959  0.04762857
#> Petal.Length   0.30328980  0.07137959   0.30458776  0.04882449
#> Petal.Width    0.04909388  0.04762857   0.04882449  0.07543265
#> 

# multiple factors
iris$Dummy <- sample(c("Hi","Lo"),150, replace=TRUE)
colMeansList(iris[,1:4], iris[,5:6])
#> $setosa.Hi
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>     4.930769     3.342308     1.476923     0.250000 
#> 
#> $versicolor.Hi
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>        5.884        2.720        4.208        1.308 
#> 
#> $virginica.Hi
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>     6.658333     3.008333     5.625000     2.066667 
#> 
#> $setosa.Lo
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>    5.0875000    3.5208333    1.4458333    0.2416667 
#> 
#> $versicolor.Lo
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>        5.988        2.820        4.312        1.344 
#> 
#> $virginica.Lo
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#>     6.523077     2.942308     5.484615     1.988462 
#>