From tlumley at u.washington.edu  Mon Jul 31 16:19:01 2006
From: tlumley at u.washington.edu (Thomas Lumley)
Date: Mon, 31 Jul 2006 07:19:01 -0700 (PDT)
Subject: [Rd] Any interest in "merge" and "by" implementations
 specifically for sorted data?
In-Reply-To: <AAC5E1FB-71BB-46C8-9D51-A72DD25C735E@sympatico.ca>
References: <Pine.GSO.4.31.0607280805550.24504-100000@markov.stats>
	<FFDE23C5-EA11-4E16-B372-012DB00BD589@sympatico.ca>
	<971536df0607281112j43c4ffc0s96d3b4743b93b690@mail.gmail.com>
	<D5EA9DD0-BFC7-45A6-A541-65E8A9E8192C@sympatico.ca>
	<Pine.GSO.4.56.0607281229290.28145@durian.statsci.com>
	<5A40DD7D-3BF4-4451-AC91-9B121BE9901F@sympatico.ca>
	<Pine.GSO.4.56.0607281725120.28145@durian.statsci.com>
	<AAC5E1FB-71BB-46C8-9D51-A72DD25C735E@sympatico.ca>
Message-ID: <Pine.LNX.4.64.0607310716250.32031@homer23.u.washington.edu>

On Sat, 29 Jul 2006, Kevin B. Hendricks wrote:

> Hi Bill,
>
>>>>    sum : igroupSums
>
> Okay, after thinking about this ...
>
> # assumes i is the small integer factor with n levels
> # v is some long vector
> # no sorting required
>
> igroupSums <- function(v,i) {
>   sums <- rep(0,max(i))
>   for (j in 1:length(v)) {
>       sums[[i[[j]]]] <- sums[[i[[j]]]] + v[[j]]
>   }
>   sums
> }
>
> if written in fortran or c might be faster than using split.  It is
> at least just linear in time with the length of vector v.

For sums you should look at rowsum().  It uses a hash table in C and last 
time I looked was faster than using split(). It returns a vector of the 
same length as the input, but that would easily be fixed.

The same approach would work for min, max, range, count, mean, but not for 
arbitrary functions.

 	-thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



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