[R] how to count number of occurrences
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Fri Nov 4 08:52:58 CET 2011
> -----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
More information about the R-help
mailing list