Hi Jon, Here is a suggestion: foo <- function(x) as.numeric( paste( abs( round( x ) ), collapse = "." ) ) testDF$value <- apply( testDF[,1:2], 1, foo ) testDF
HTH, Jorge On Wed, Sep 16, 2009 at 3:22 PM, Jon Loehrke <jloeh...@umassd.edu> wrote: > 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)) > > # 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 > jloeh...@umassd.edu > T 508-910-6393 > F 508-910-6396 > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.