Skip to content
Back to formatted view

Raw Message

Message-ID: <941871A13165C2418EC144ACB212BDB0021B2065@dshsmxoly1504g.dshs.wa.lcl>
Date: 2011-11-04T07:52:58Z
From: Nordlund, Dan (DSHS/RDA)
Subject: how to count number of occurrences
In-Reply-To: <1320361776236-3988529.post@n4.nabble.com>

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of uka
> Sent: Thursday, November 03, 2011 4:10 PM
> To: r-help at r-project.org
> Subject: Re: [R] how to count number of occurrences
> 
> This was very helpful. Thank you very much. Just one question, I notice
> that
> it does not count the number of X's before the first Y. I want the
> result be
> 1 4 0 0 0 5 0 0 0 0. I tried combining this output with the first value
> of
> rle output, but realized that rle doesn't give me the 0s. So, if my
> first
> observation was Y, then I want it to show that there are 0 Xs before
> that.
> Thank you again.
> 

You should really provide the relevant context from previous posts so that potential helpers don't need to go looking for it.  That being said, you could try something like

samp <- c("X", "Y", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "X", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "Y")
diff(which(c('Y', samp)=='Y'))-1 


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204