Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

tapplySimpl

More simplification in tapply() result


Description

For the case of more than two categories or indices (in INDEX), traditional tapply(*, simplify = TRUE) still returns a list when an array may seem more useful and natural. This is provided by tapplySimpl() if the function FUN() is defined such as to return a vector of the same length in all cases.

Usage

tapplySimpl(X, INDEX, FUN, ...)

Arguments

X

an atomic object, typically a vector. All these arguments are as in tapply() and are passed to tapply(..).

INDEX

list of (typically more than one) factors, each of same length as X.

FUN

the function to be applied. For the result to be simplifiable, FUN() must return a vector of always the same length.

...

optional arguments to FUN.

Value

If the above conditions are satisfied, the list returned from r <- tapply(X, INDEX, FUN, ...) is simplified into an array of rank 1 + \#\{indices\}, i.e., 1+length(INDEX); otherwise, tapplySimpl() returns the list r, i.e., the same as tapply().

Author(s)

Martin Maechler, 14 Jun 1993 (for S-plus).

See Also

tapply(*, simplify=TRUE).

Examples

## Using tapply() would give a list (with dim() of a matrix);
  ## here we get 3-array:

  data(esoph)
  with(esoph, {
       mima <<- tapplySimpl(ncases/ncontrols, list(agegp, alcgp),  range)
       stopifnot(dim(mima) == c(2, nlevels(agegp), nlevels(alcgp)))
       })
  aperm(mima)

sfsmisc

Utilities from 'Seminar fuer Statistik' ETH Zurich

v1.1-11
GPL (>= 2)
Authors
Martin Maechler [aut, cre] (<https://orcid.org/0000-0002-8685-9910>), Werner Stahel [ctb] (Functions: compresid2way(), f.robftest(), last(), p.scales(), p.dnorm()), Andreas Ruckstuhl [ctb] (Functions: p.arrows(), p.profileTraces(), p.res.2x()), Christian Keller [ctb] (Functions: histBxp(), p.tachoPlot()), Kjetil Halvorsen [ctb] (Functions: KSd(), ecdf.ksCI()), Alain Hauser [ctb] (Functions: cairoSwd(), is.whole(), toLatex.numeric()*), Christoph Buser [ctb] (to function Duplicated()), Lorenz Gygax [ctb] (to function p.res.2fact()), Bill Venables [ctb] (Functions: empty.dimnames(), primes()), Tony Plate [ctb] (to inv.seq()), Isabelle Fl<fc>ckiger [ctb], Marcel Wolbers [ctb], Markus Keller [ctb], Sandrine Dudoit [ctb], Jane Fridlyand [ctb], Greg Snow [ctb] (to loessDemo()), Henrik Aa. Nielsen [ctb] (to loessDemo()), Vincent Carey [ctb], Ben Bolker [ctb], Philippe Grosjean [ctb], Fr<e9>d<e9>ric Ibanez [ctb], Caterina Savi [ctb], Charles Geyer [ctb], Jens Oehlschl<e4>gel [ctb]
Initial release
2021-04-03

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.