Message-ID: <CAM_vju=qd6kVCJCJU5S132HokL5bHdbi06fCPZsW_hJCCs-_tA@mail.gmail.com>
Date: 2011-11-02T18:08:42Z
From: Sarah Goslee
Subject: how to count number of occurrences
In-Reply-To: <CAFciP4Xwa+b3iZnv--JhYyEex0BTFB1sunRVLQmFaAY8rtsp_g@mail.gmail.com>
Hi,
On Wed, Nov 2, 2011 at 12:54 PM, Sl K <s.karmv at gmail.com> wrote:
> Dear R users,
>
> I have this data frame,
> ? ? ? ? ? y samp
> 8 0.03060419 ? ?X
> 18 0.06120838 ? ?Y
> 10 0.23588374 ? ?X
> 3 0.32809965 ? ?X
> 1 ?0.36007100 ? ?X
> 7 0.36730571 ? ?X
> 20 0.47176748 ? ?Y
> 13 0.65619929 ? ?Y
> 11 0.72014201 ? ?Y
> 17 0.73461142 ? ?Y
> 6 0.76221313 ? ?X
> 2 0.77005691 ? ?X
> 4 0.92477243 ? ?X
> 9 0.93837591 ? ?X
> 5 0.98883581 ? ?X
> 16 1.52442626 ? ?Y
> 12 1.54011381 ? ?Y
> 14 1.84954487 ? ?Y
> 19 1.87675183 ? ?Y
> 15 1.97767162 ? ?Y
>
> and I am trying to find the number of X's that occur before ith Y occurs.
> For example, there is 1 X before the first Y, so I get 1. There are 4 X's
> before the second Y, so I get 4, there is no X between second and third Y,
> so I get 0 and so on. Any hint to at least help me to start this will be
> appreciated. Thanks a lot!
Using dput() to provide reproducible data would be nice, but failing that
here's a simple example with sample data:
> testdata <- c("x", "y", "x", "x", "x", "y", "x", "x", "x", "x", "x", "y", "y")
> rle(testdata)
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.
> rle(testdata)$lengths[rle(testdata)$values == "x"]
[1] 1 3 5
--
Sarah Goslee
http://www.functionaldiversity.org