Re: [R] Average regions of non-zeros

2010-03-08 Thread Linlin Yan
Nice shot of cumsum(). Just improve it a little: > x <- c(0,0,1,2,3,0,0,4,5,6) > x.groups <- split(x, (x != 0) * cumsum(x == 0))[-1] > x.groups $`2` [1] 1 2 3 $`4` [1] 4 5 6 > lapply(x.groups, mean) $`2` [1] 2 $`4` [1] 5 On Mon, Mar 8, 2010 at 11:02 AM, jim holtman wrote: > Try this: > >> x <

Re: [R] Average regions of non-zeros

2010-03-08 Thread jim holtman
e explain that? > > Thanks, > > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] > On > Behalf Of jim holtman > Sent: 08 March 2010 08:32 > To: Daren Tan > Cc: r-h...@stat.math.ethz.ch > Subject: Re: [R] Average r

Re: [R] Average regions of non-zeros

2010-03-08 Thread bogaso.christofer
Behalf Of jim holtman Sent: 08 March 2010 08:32 To: Daren Tan Cc: r-h...@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

Re: [R] Average regions of non-zeros

2010-03-07 Thread Dimitris Rizopoulos
try this: x <- c(0,0,1,2,3,0,0,4,5,6) rl <- rle(x == 0) grp <- rep(seq_along(rl$lengths), rl$lengths) res <- tapply(x, grp, mean) res[res > 0] I hope it helps. Best, Dimitris On 3/8/2010 3:48 AM, Daren Tan wrote: x<- c(0,0,1,2,3,0,0,4,5,6) How to identify the regions of non-zeros and a

Re: [R] Average regions of non-zeros

2010-03-07 Thread jim holtman
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

[R] Average regions of non-zeros

2010-03-07 Thread Daren Tan
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