[R] How many times occurs
Rainer Schuermann
Rainer.Schuermann at gmx.net
Sun Jul 3 08:53:37 CEST 2011
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")))
More information about the R-help
mailing list