[R] How many times occurs
Weidong Gu
anopheles123 at gmail.com
Sun Jul 3 19:28:45 CEST 2011
another way of thinking is to turn the sequences into strings
if your data is db
>apply(db,1,function(x) length(gregexpr('892',paste(c(x),collapse=''))[[1]]))
[1] 1 3 1 1 3 2
Weidong Gu
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.
>
> I tried this, but doesn´t works....also tried other thinks but also the same
> results:
>
> *dat<-read.table('input1.txt')*
> **
> **
> *dat1 <- dat[ dat[,1]=8 & dat[,2]=9 & dat[,3]=2 ,]=1*
> *dat2<-dat[(dat[,2]= 8 & dat[,3]=9 & dat[,4]=2),]=1*
> *dat3<-dat[(dat[,5]=8 & dat[,4]=9 & dat[,5]=2),]=1*
> *dat4<-dat[(dat[,4]=8 & dat[,5]=9 & dat[,6]=2),]=1*
> *dat5<-dat[(dat[,5]=8 & dat[,6]=9 & dat[,7]=2),]=1*
> *dat6<-dat[(dat[,6]=8 & dat[,7]=9 & dat[,8]=2),6]=1*
> *dat7<-dat[(dat[,7]=8 & dat[,8]=9 & dat[,9]=2),7]=1*
> *dat8<-dat[(dat[,8]=8 & dat[,9]=9 & dat[,10]=2),8]=1*
> **
> datfinal<-dat1+da2+dat3+dat4+dat5+dat6+dat7+dat8
>
> final2 <- dat[ rowSums(datfinal) < 2 , ]
>
> So my last matrix "final2" will be "dat" without the rows that doesn´t pass
> the conditions.
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
More information about the R-help
mailing list