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]]
On Saturday 02 July 2011 21:40:24 Trying To learn again wrote: Clumsy but it works (replace the bingo stuff with what you want to do next): x <- as.matrix( read.table( "input.txt") ) xdim <- dim( x ) ix <- xdim[ 1 ] jx <- xdim[ 2 ] - 2 bingo <- 0 for( i in 1:ix ) { for( j in 1:jx ) { if( x[i,j] == 8 && x[i,j+1] == 9 && x[i,j+2] == 2 ) { bingo <- bingo + 1 } } } print( bingo ) I'm sure there are more elegant and efficient solutions! Rgds, Rainer Here the matrix as dput( x ): structure(c(8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 8L, 5L, 5L, 8L, 5L, 4L, 9L, 4L, 4L, 9L, 4L, 5L, 2L, 5L, 5L, 2L, 5L, 8L, 8L, 8L, 8L, 8L, 8L, 5L, 9L, 5L, 5L, 9L, 9L, 6L, 2L, 6L, 6L, 2L, 2L, 6L, 1L, 4L, 6L, 1L, 2L), .Dim = c(6L, 10L), .Dimnames = list(NULL, c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10")))
I suppose the easiest way to make this is to use loops and construct a zero matrix to be completed each time the sequence accompleixes, don´t you? I will try this evening¡¡¡ 2011/7/2 Trying To learn again <tryingtolearnagain@gmail.com>> 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]]
Sorry, I forgot to attach the original post, so here once more with a cosmetic change: x <- as.matrix( read.table( "matr.txt") ) bingo <- 0 for( i in 1:dim( x )[1] ) { for( j in 1:dim( x )[2] - 2 ) { if( x[i,j] == 8 && x[i,j+1] == 9 && x[i,j+2] == 2 ) { bingo <- bingo + 1 } } } print( bingo ) Rgds, Rainer On Saturday 02 July 2011 21:40:24 Trying To learn again 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]] >dput( x ): structure(c(8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 8L, 5L, 5L, 8L, 5L, 4L, 9L, 4L, 4L, 9L, 4L, 5L, 2L, 5L, 5L, 2L, 5L, 8L, 8L, 8L, 8L, 8L, 8L, 5L, 9L, 5L, 5L, 9L, 9L, 6L, 2L, 6L, 6L, 2L, 2L, 6L, 1L, 4L, 6L, 1L, 2L), .Dim = c(6L, 10L), .Dimnames = list(NULL, c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10")))
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
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. > >