[R] How many times occurs

Rainer Schuermann Rainer.Schuermann at gmx.net
Sun Jul 3 09:32:53 CEST 2011


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")))



More information about the R-help mailing list