[R] How to count the number of sequences in matrix?

James W. MacDonald jmacdon at med.umich.edu
Tue Nov 13 22:01:22 CET 2007


Hi Lauri,

A quick hack:

a <- lapply(g, 1, rle)
b <- sapply(a, function(x), x[[1]][x[[2]] == 1])
d <- cbind(rep(1:length(b), sapply(b, length)),
            rep(sapply(b, length), sapply(b, length)),
            unlist(b))

Best,

Jim




Lauri Nikkinen wrote:
> 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 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.

-- 
James W. MacDonald, M.S.
Biostatistician
Affymetrix and cDNA Microarray Core
University of Michigan Cancer Center
1500 E. Medical Center Drive
7410 CCGC
Ann Arbor MI 48109
734-647-5623



More information about the R-help mailing list