Here's one way...

Not tested, so there maybe typos and such, but I've used this approach successfully quite a few times.

It can get kind of slow if dat1 has many, many rows.
The coding assumes no missing data, though that could be handled by adding the na.rm argument in apppropriate places, and changing the nrow() to something that counts only non-missing data.


myfun <- function(dfr) {
  data.frame(
             wshed=dfr$wshed[1],
             site=dfr$site[1],
             species=dfr$species[1],
             mBda=mean(dfr$BdA),
             cBda=sd(dfr$Bda)/mean(dfr$Bda),
             sBda=sd(dfr$Bda)/sqrt(nrow(dfr)),
             nBda=nrow(dfr),
             msla=mean(dfr$BdA),
             csla=sd(dfr$sla)/mean(dfr$sla),
             ssla=sd(dfr$sla)/sqrt(nrow(dfr)),
             nsla=nrow(dfr))
}

tmp1 <- split(dat1,paste(dat1$wshed,dat1$site,dat1$species))
tmp2 <- lapply(tmp1,myfun)
dat2 <- do.call('rbind',tmp2)

-Don

At 6:18 PM -0400 7/16/04, Greg Adkison wrote:
I would be incredibly grateful to anyone who'll help me translate some
SAS code into R code.

Say for example that I have a dataset named "dat1" that includes five
variables:  wshed, site, species, bda, and sla.  I can calculate with the
following SAS code the mean, CV, se, and number of observations of
"bda" and "sla" for each combination of "wshed," "species," and "site,"
restricting the species considered to only three of several species in
dat1 (b, c, and p).  Moreover, I can output these calculations and
grouping variables to a dataset named "dat2" that will reside in RAM
and include the variables  wshed, site, species, mBdA, msla, cBda,
sBdA, ssla, nBda, and nsla.

proc sort data=dat1;
  by wshed site species;
proc means data=dat1 noprint mean cv stderr n;
  by wshed site species;
  where species in ('b', 'c', 'p');
  var BdA sla;
  output out=dat2
    mean=mBdA msla
    cv=cBdA csla
    stderr=sBdA ssla
    n=nBdA nsla;

Thanks,
Greg

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to