[R] Count number of consecutive zeros by group

arun smartpink111 at yahoo.com
Fri Nov 1 14:17:19 CET 2013


I think this gives a different result than the one OP asked for:

df1 <- structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), x = c(1, 0, 
0, 1, 0, 0, 0, 1, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0)), .Names = c("ID", 
"x"), row.names = c(NA, -22L), class = "data.frame")

with(df1, sapply(split(x, ID), function(x) sum(x==0)))

with(df1,tapply(x,list(ID),function(y) {rl <- rle(!y); max(c(0,rl$lengths[rl$values]))}))


A.K.


On Friday, November 1, 2013 6:01 AM, PIKAL Petr <petr.pikal at precheza.cz> wrote:
Hi

Another option is sapply/split/sum construction

with(data, sapply(split(x, ID), function(x) sum(x==0)))

Regards
Petr


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Carlos Nasher
> Sent: Thursday, October 31, 2013 6:46 PM
> To: S Ellison
> Cc: r-help at r-project.org
> Subject: Re: [R] Count number of consecutive zeros by group
> 
> If I apply your function to my test data:
> 
> ID <- c(1,1,1,2,2,3,3,3,3)
> x <- c(1,0,0,0,0,1,1,0,1)
> data <- data.frame(ID=ID,x=x)
> rm(ID,x)
> 
> f2 <-   function(x) {
>   max( rle(x == 0)$lengths )
> }
> with(data, tapply(x, ID, f2))
> 
> the result is
> 1 2 3
> 2 2 2
> 
> which is not what I'm aiming for. It should be
> 1 2 3
> 2 2 1
> 
> I think f2 does not return the max of consecutive zeros, but the max of
> any consecutve number... Any idea how to fix this?
> 
> 
> 2013/10/31 S Ellison <S.Ellison at lgcgroup.com>
> 
> >
> >
> > > -----Original Message-----
> > > So I want to get the max number of consecutive zeros of variable x
> > > for
> > each
> > > ID. I found rle() to be helpful for this task; so I did:
> > >
> > > FUN <- function(x) {
> > >   rles <- rle(x == 0)
> > > }
> > > consec <- lapply(split(df[,2],df[,1]), FUN)
> >
> > You're probably better off with tapply and a function that returns
> > what you want. You're probably also better off with a data frame name
> > that isn't a function name, so I'll use dfr instead of df...
> >
> > dfr<- data.frame(x=rpois(500, 1.5), ID=gl(5,100)) #5 ID groups
> > numbered 1-5, equal size but that doesn't matter for tapply
> >
> > f2 <-   function(x) {
> >         max( rle(x == 0)$lengths )
> > }
> > with(dfr, tapply(x, ID, f2))
> >
> >
> > S Ellison
> >
> >
> > *******************************************************************
> > This email and any attachments are confidential. Any
> > u...{{dropped:24}}
> 
> ______________________________________________
> R-help at 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.


______________________________________________
R-help at 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.




More information about the R-help mailing list