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 <dare...@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>
> 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]]

______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to