What I was looking for was the string of non-zero values and where they
'broke' at.  I could have used 'rle', but I sometime find this approach just
as easy.  Every place there is a zero will be TRUE which has the value 1.
'cumsum' will generate a running sum of these values.  When there is a
non-zero value, you will get consecutive values of cumsum to be the same.
The is what you saw in pasting the value into the window.  Notice that the
run of '2's begins with a value of zero and then includes all the non-zero
values following.  By using 'split', I cn create a list of each group.  If
the group is of length 1, then it only contains zero and I ignore it.  If
the length is greater than 1, then we have some non-zero values and we have
to throw away the leading zero in the group (tail(a, -1)) and then take the
mean.

HTH

On Mon, Mar 8, 2010 at 3:26 AM, bogaso.christofer <
bogaso.christofer@gmail.com> wrote:

> Hi Jim I was following this thread and found that your answer is perfect
> there. However I could not comprehend the meaning of the expression "
> cumsum(x == 0)". If I paste it in R window, I get following :
>
> > cumsum(x == 0)
>  [1] 1 2 2 2 2 3 4 4 4 4
>
> I gone through the help page of cumsum() function I correctly understand
> that this function calculates the cumulative sum. But could not understand
> really the meaning of cumsum(x == 0)
>
> Would you please explain that?
>
> Thanks,
>
> -----Original Message-----
> From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org]
> On
> Behalf Of jim holtman
> Sent: 08 March 2010 08:32
> To: Daren Tan
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] Average regions of non-zeros
>
> Try this:
>
> > x <- c(0,0,1,2,3,0,0,4,5,6)
> > # partition the data
> > x.p <- split(x, cumsum(x == 0))
> > # now only process groups > 1
> > x.mean <- lapply(x.p, function(a){
> +     if (length(a) == 1) return(NULL)
> +     return(list(grp=tail(a, -1), mean=mean(tail(a, -1))))
> + })
> > # now only return the real values
> > x.mean[unlist(lapply(x.mean, length) != 0)]
> $`2`
> $`2`$grp
> [1] 1 2 3
> $`2`$mean
> [1] 2
>
> $`4`
> $`4`$grp
> [1] 4 5 6
> $`4`$mean
> [1] 5
>
>
>
> On Sun, Mar 7, 2010 at 9:48 PM, Daren Tan <daren76@hotmail.com> wrote:
>
> >
> > x <- c(0,0,1,2,3,0,0,4,5,6)
> >
> >
> >
> > How to identify the regions of non-zeros and average c(1,2,3) and
> c(4,5,6)
> > to get 2 and 5.
> >
> >
> >
> > Thanks
> >
> >
> >
> > _________________________________________________________________
> > Hotmail: Trusted email with Microsoft s powerful SPAM protection.
> >
> >        [[alternative HTML version deleted]]
> >
> >
> > ______________________________________________
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> >
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> <http://www.r-project.org/posting
> -guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
> >
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>
>        [[alternative HTML version deleted]]
>
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

	[[alternative HTML version deleted]]

