one approach is the following: res <- apply(g, 1, function (x) { out <- rle(as.logical(x)) out <- out$lengths[out$values] ln <- length(out) c(rep(ln, ln), out) })
cbind(rep(1:nrow(g), sapply(res, length) / 2), do.call("rbind", sapply(res, matrix, ncol = 2))) I hope it helps. Best, Dimitris ----- Original Message ----- From: "Lauri Nikkinen" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, November 13, 2007 2:07 PM Subject: [R] How to count the number of sequences in matrix? > Hi R-users, > > I have a matrix similar to this. I would like to calculate the > number > of 1 sequences in a row and also the length of the 1 sequence. For > instance, in the example below, row number one has four sequences of > number 1 (from left to right: 1; 1; 1,1; 1) and the corresponding > SeqCount is 4 and SeqLenght 1,1,2,1. > > g <- c(0,0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1 ,1 ,0 ,1 ,1 ,1 ,0 > ,1 > ,1 ,0 ,1 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,1 ,1 ,1 ,0 ,1 ,0 ,1 ,1 ,1 ,1 ,0 ,1 ,1 > ,1 ,1 ,0 ,1 ,1 ,0 ,1 ,0 ,0 ,1 ,0 ,0 ,0 ,1 ,0 ,1 ,0 ,0 ,1 ,1 ,0 ,1 ,1 > ,0 ,0 ,1 ,0 ,1 ,0 ,0 ,1 ,1 ,0 ,1 ,1 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,1 ,0 ,1 ,1 > ,0 ,0 ,1 ,0 ,0 ,1 ,1 ,0 ,1) > dim(g) <- c(10,10) > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,] 0 1 0 0 1 0 1 1 0 1 > [2,] 0 1 1 1 1 1 0 0 0 0 > [3,] 1 0 1 1 0 0 0 1 0 0 > [4,] 1 1 0 1 1 0 1 0 0 1 > [5,] 0 1 1 1 1 1 1 0 0 0 > [6,] 1 1 1 0 1 0 0 1 1 0 > [7,] 1 0 0 1 1 0 1 1 1 1 > [8,] 1 1 1 0 0 0 1 0 1 1 > [9,] 1 1 0 1 1 1 0 1 0 0 > [10,] 1 1 1 1 1 0 0 1 1 1 > > The resulting datafmame should look like this: > x <- "Rownumber SeqCount SeqLenght > 1 4 1 > 1 4 1 > 1 4 2 > 1 4 1 > 2 1 5 > 3 3 1 > 3 3 2 > 3 3 1 > 4 4 2 > 4 4 2 > 4 4 1 > 4 4 1 > 5 1 6 > 6 3 3 > 6 3 1 > 6 3 2 > 7 3 1 > 7 3 2 > 7 3 4 > 8 3 3 > 8 3 1 > 8 3 2 > 9 3 2 > 9 3 3 > 9 3 1 > 10 2 5 > 10 2 3" > x <- read.table(textConnection(x), header=TRUE) > x > > Any idea how to do this in R? > > Best regards, > Lauri > > ______________________________________________ > R-help@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. > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm ______________________________________________ R-help@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.