From tshort at eprisolutions.com  Mon Jul 31 17:57:43 2006
From: tshort at eprisolutions.com (tshort)
Date: Mon, 31 Jul 2006 08:57:43 -0700 (PDT)
Subject: [Rd] Any interest in "merge" and "by" implementations
 specifically for so
In-Reply-To: <A37700AE-CA2E-42E9-B734-EEC0A7420569@sympatico.ca>
References: <07820D54-9DDC-42F7-8F32-C931B2B80DE6@sympatico.ca>
	<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>
	<207048BB-90DD-43DA-8FBC-D73544E6E3A8@sympatico.ca>
	<5562816.post@talk.nabble.com>
	<A37700AE-CA2E-42E9-B734-EEC0A7420569@sympatico.ca>
Message-ID: <5578580.post@talk.nabble.com>


> Hi Tom,
> 
> > Now, try sorting and using a loop:
> >
> >> idx <- order(i)
> >> xs <- x[idx]
> >> is <- i[idx]
> >> res <- array(NA, 1e6)
> >> idx <- which(diff(is) > 0)
> >> startidx <- c(1, idx+1)
> >> endidx <- c(idx, length(xs))
> >> f1 <- function(x, startidx, endidx, FUN = sum)  {
> > +   for (j in 1:length(res)) {
> > +     res[j] <- FUN(x[startidx[j]:endidx[j]])
> > +   }
> > +   res
> > + }
> >> unix.time(res1 <- f1(xs, startidx, endidx))
> > [1] 6.86 0.00 7.04   NA   NA
> 
> I wonder how much time the sorting, reordering and creation os  
> startidx and endidx would add to this time?

Done interactively, sorting and indexing seemed fast. Here are some timings:

> unix.time({idx <- order(i)
+            xs <- x[idx]
+            is <- i[idx]
+            res <- array(NA, 1e6)
+            idx <- which(diff(is) > 0)
+            startidx <- c(1, idx+1)
+            endidx <- c(idx, length(xs))
+          })
[1] 1.06 0.00 1.09   NA   NA


> That looks interesting.  Does it only work for specific operating  
> systems and processors?  I will give it a try.

No, as far as I know, it works on all operating systems. Also, it gets a
little faster if you directly put the sum in the function:

> f4 <- function(x, startidx, endidx)  {
+   for (j in 1:length(res)) {
+     res[j] <- sum(x[startidx[j]:endidx[j]])
+   }
+   res
+ }
> f5 <- cmpfun(f4)
> unix.time(res5 <- f5(xs, startidx, endidx))
[1] 2.67 0.03 2.95   NA   NA

- Tom


-- 
View this message in context: http://www.nabble.com/Any-interest-in-%22merge%22-and-%22by%22-implementations-specifically-for-sorted-data--tf2009595.html#a5578580
Sent from the R devel forum at Nabble.com.



[Rd] Any interest in "merge" and "by" implementations specifically for so
tshort