Description Usage Arguments Details Value Note Author(s) References See Also Examples
Like calling tapply
but stretching the result to dimensions of x
.
1 |
x |
an atomic object, typically a vector |
INDEX |
list of factors, each of same length as |
FUN |
a function that accepts a vector and returns a vector |
where |
logical, restricting the domain of x |
... |
optional arguments to |
The function tapply
applies FUN
to each cell of a vector, as
specified by levels of INDEX
. reapply
repeats that result as necessary
to match the number of input elements per cell, and restores the order to that
of the original index. Regardless of the length of the value of FUN
, the
length of the value of reapply is always identical to that of x
.
If where
is specified, elements in x corresponding to FALSE
are
not passed to FUN
. Argument where
is recycled as necessary and coerced
to logical, with NA set to TRUE
.
reapply
gives a result similar to stats::ave
. However,
ave
gives a warning if the input for a cell is not a multiple of the output.
Also, reapply
has a named argument for the index
variables, and passes dots to fun
; for ave
, dots represent the indices,
and any extra arguments to fun
need to be hard-coded, e.g. in an anonymous
function (see examples for ave
). Finally, reapply
returns NA for any
element where some member of INDEX is NA, whereas ave
applies FUN
to
these elements in isolation.
an atomic object, typically a vector
Caution! If FUN
is isometric (output has same length as input, e.g. cumsum
but not mean
) then
use of where
may give surprising results. If where
is ever FALSE
,
then the input to FUN
will be necessarily shorter than the required output,
and recycling will be in effect.
Tim Bergsma
http://metrumrg.googlecode.com
tapply
lapply
rep
ave
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | theoph <- Theoph[c(1,12,2,13,3,14),]
theoph$avg <- with(theoph,reapply(conc,Subject,mean))
theoph$avg2 <- with(theoph,reapply(conc,Subject,mean,where=Time!=0,na.rm=TRUE))
theoph$sum <- with(theoph,reapply(conc,Subject,cumsum))
theoph
Theoph <- Theoph[Theoph$Subject %in% c(1,2),]
Theoph <- Theoph[distance(within=Theoph$Subject) < 4,]
Theoph$Subject <- as.character(Theoph$Subject)
Theoph$where <- TRUE
Theoph <- within(Theoph, where[Subject == 2 & conc == 0] <- FALSE)
Theoph <- rbind(Theoph, aug(Theoph[1,],Subject = NA))
Theoph$misc <- reapply(Theoph$conc,INDEX=Theoph[,c('Subject','Dose')],
where=Theoph$where,FUN= `[`,1:3)
Theoph
stopifnot(class(reapply(c('foo','bar'),INDEX=c(1,1),FUN=function(x)x=='foo'))=='logical')
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.