From sfalcon at fhcrc.org  Thu Jul 27 16:20:29 2006
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Thu, 27 Jul 2006 07:20:29 -0700
Subject: [Rd] Any interest in "merge" and "by" implementations
	specifically for sorted data?
In-Reply-To: <07820D54-9DDC-42F7-8F32-C931B2B80DE6@sympatico.ca> (Kevin B.
	Hendricks's message of "Thu, 27 Jul 2006 09:31:03 -0400")
References: <07820D54-9DDC-42F7-8F32-C931B2B80DE6@sympatico.ca>
Message-ID: <m2zmevceoi.fsf@ziti.local>

"Kevin B. Hendricks" <kevin.hendricks at sympatico.ca> writes:
> My first R attempt was a simple
>
> # sort the data.frame gd and the sort key
> sorder <- order(MDPC)
> gd  <- gd[sorder,]
> MDPC <- MDPC[sorder]
> attach(gd)
>
> # find the length and sum for each unique sort key
> XN <- by(MVE, MDPC, length)
> XSUM <- by(MVE, MDPC, sum)
> GRPS <- levels(as.factor(MDPC))
>
> Well the ordering and sorting was reasonably fast but the first "by"  
> statement was still running 4 hours later on my machine (a dual 2.6  
> gig Opteron with 4 gig of main memory).  This same snippet of code in  
> SAS running on a slower machine takes about 5 minutes of system
> time.

I wonder if split() would be of use here.  Once you have sorted the
data frame gd and the sort keys MDPC, you could do:

gdList <- split(gd$MVE, MDPC)

xn <- sapply(gdList, length)
xsum <- sapply(gdList, sum)

+ seth



[Rd] Any interest in "merge" and "by" implementations specifically for sorted data?
Seth Falcon