[R] apply function across two variables by mult factors
Erik Iverson
eiverson at NMDP.ORG
Wed Sep 16 21:43:53 CEST 2009
Hello,
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Jon Loehrke
> Sent: Wednesday, September 16, 2009 2:23 PM
> To: r-help at r-project.org
> Subject: [R] apply function across two variables by mult factors
>
> Greetings,
>
> I am attempting to run a function, which produces a vector and
> requires two input variables, across two nested factor levels. I can
> do this using by(X, list(factor1, factor2), function), however I
> haven't found a simple way to extract the list output into an
> organized vector form. I can do this using nested loops but it isn't
> exactly an optimal approach.
>
> Thank you for any and all suggestions. Jon
>
> # 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]))
> # example function [trivial]
> testFun<-function(x){
> X<-abs(x[,1]);
> Y<-abs(x[,2]);
> as.numeric( paste(round(X), round(Y), sep='.'))
> }
>
> # apply by factor levels but hard to extract values
> by(testDF[,1:2], list(testDF$f1, testDF$f2), testFun)
>
> # Loop works, but not efficient for large datasets
> testDF$value<-NA
> for(i in levels(testDF$f1)){
> for(j in levels(testDF$f2)){
> testDF[testDF$f1==i & testDF$f2==j,]$value<-
> testFun(testDF[testDF
> $f1==i & testDF$f2==j,1:2])
> }
> }
> testDF
> sessionInfo()
> #R version 2.9.1 Patched (2009-08-07 r49093)
> #i386-apple-darwin8.11.1
> #
> #locale:
> #en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
> #
> #attached base packages:
> #[1] stats graphics grDevices utils datasets methods base
>
>
> Jon Loehrke
> Graduate Research Assistant
> Department of Fisheries Oceanography
> School for Marine Science and Technology
> University of Massachusetts
> 200 Mill Road, Suite 325
> Fairhaven, MA 02719
> jloehrke at umassd.edu
> T 508-910-6393
> F 508-910-6396
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list