[R] How many times occurs

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jul 3 14:00:36 CEST 2011


On Sat, Jul 2, 2011 at 3:40 PM, Trying To learn again
<tryingtolearnagain at gmail.com> wrote:
> Hi all,
>
> I have a data matrix likein "input.txt"
>
> 8 9 2 5 4 5 8 5 6 6
> 8 9 2 8 9 2 8 9 2 1
> 8 9 2 5 4 5 8 5 6 4
>  8 9 2 5 4 5 8 5 6 6
> 8 9 2 8 9 2 8 9 2 1
> 8 9 2 5 4 5 8 9 2   2
>
>
> In this example will be an  6x10 matrix (or data frame)
>
> I want to detect how many times in a row appears this combination  8 follewd
> by 9 followed by 2, and create a new matrix with only this number of occurs
> then if this number is less than "n" I keep the row. For example in the
> last row the number n will be 2 because "series" 8 9 2 appears 2 times in
> the same row.
>

For each column of t(dat) apply the indicated function to successive
triples and
then sum the resulting columns:

> library(zoo)
> colSums(rollapply(zoo(t(dat)), 3, function(x) all(x == c(8, 9, 2))))
[1] 1 3 1 1 3 2

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list