[R] sas to r

Don MacQueen macq at llnl.gov
Sat Jul 17 00:58:08 CEST 2004

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) {

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


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;
>R-help at stat.math.ethz.ch mailing list
>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

More information about the R-help mailing list