Re: [R] sas to r

From: Don MacQueen <>
Date: Sat 17 Jul 2004 - 08:58:08 EST

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 <-'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;
> mailing list
>PLEASE do read the posting guide!

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

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Sat Jul 17 09:07:10 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:55:03 EST