[R] interaction of shingles and tapply()
Roger Levy
rlevy at ucsd.edu
Wed Nov 28 02:56:03 CET 2007
I'm interested in a version of tapply() that operates with shingles
instead of factors. For instance:
x <- c(1,1,2,2,3,3)
y <- c(1,1,1,0,0,0)
s <- shingle(x,intervals=cbind(c(0.5,1.5),c(2.5,3.5)))
# the following function should exist!
tapply.shingle(x,s,mean) # returns the vector c(0.75,0.25)
I've written such a function as follows:
tapply.shingle <- function(x,s,fn,...) {
result <- c()
for(l in levels(s)) {
x1 <- x[s > l[1] & s < l[2]]
result <- c(result, fn(x1,...))
}
result
}
However, I'm not thrilled with the for() loop, and I don't see any way
to generalize this function to handle lists of shingles instead of
individual shingles, except to use recursion. Does anyone have any
suggestions or thoughts?
Many thanks,
Roger
More information about the R-help
mailing list