[R] apply function across two variables by mult factors
Erik Iverson
eiverson at NMDP.ORG
Wed Sep 16 21:47:08 CEST 2009
One correction below,
---snip---
> >
> > # example data frame
> > testDF<-data.frame(
> > x=rnorm(12),
> > y=rnorm(12),
> > f1=gl(3,4),
> > f2=gl(2,2,12))
> >
>
> Try this using lapply, split, mapply? Maybe it is in a nicer output
> object for you?
>
> testFun2 <- function(x, y) {
> X <- abs(x);
> Y <- abs(y);
> as.numeric(paste(round(X), round(Y), sep='.'))
> }
>
> lapply(split(testDF, list(testDF$f1, testDF$f2)),
> function(x) mapply(testFun2, x[1], x[2]))
>
Or use "list indexing" in the mapply call to get a vector, in this case at least...
lapply(split(testDF, list(testDF$f1, testDF$f2)),
function(x) mapply(testFun2, x[[1]], x[[2]]))
---snip---
More information about the R-help
mailing list