Skip to content

how to count number of occurrences

6 messages · Sl K, Sarah Goslee, William Dunlap +3 more

#
Hi,
On Wed, Nov 2, 2011 at 12:54 PM, Sl K <s.karmv at gmail.com> wrote:
Using dput() to provide reproducible data would be nice, but failing that
here's a simple example with sample data:
Run Length Encoding
  lengths: int [1:6] 1 1 3 1 5 2
  values : chr [1:6] "x" "y" "x" "y" "x" "y"

You can use the values component of the list returned by rle to subset the
lengths component of the list to get only the x values if that's what you
need to end up with.
[1] 1 3 5
#
Is the following what you want?  It should give
the number of "X"s immediately preceding each "Y".
"X", "X", "X", "Y", "Y", "Y", "Y", "Y")
[1] 4 0 0 0 5 0 0 0 0

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
On Nov 2, 2011, at 12:54 PM, Sl K wrote:

            
dat$nXs <- cumsum(dat$samp=="X")
  dat$nYs <- cumsum(dat$samp=="Y")
  dat
#
             y samp nXs nYs
8  0.03060419    X   1   0
18 0.06120838    Y   1   1
10 0.23588374    X   2   1
3  0.32809965    X   3   1
1  0.36007100    X   4   1
7  0.36730571    X   5   1
20 0.47176748    Y   5   2
13 0.65619929    Y   5   3
11 0.72014201    Y   5   4
17 0.73461142    Y   5   5
6  0.76221313    X   6   5
2  0.77005691    X   7   5
4  0.92477243    X   8   5
9  0.93837591    X   9   5
5  0.98883581    X  10   5
16 1.52442626    Y  10   6
12 1.54011381    Y  10   7
14 1.84954487    Y  10   8
19 1.87675183    Y  10   9
15 1.97767162    Y  10  10

I find that there are 5 X's before the second Y.

 > nXbefore_mthY <- function(m) dat[which(dat$nYs==m), "nXs"]
 > nXbefore_mthY(2)
[1] 5
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
1 day later
uka
#
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.

--
View this message in context: http://r.789695.n4.nabble.com/how-to-count-number-of-occurrences-tp3979546p3988529.html
Sent from the R help mailing list archive at Nabble.com.
#
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