[R] Counting zeros in a matrix
Ben Fairbank
BEN at SSANET.COM
Tue Nov 28 23:00:02 CET 2006
rowSums((A[,1:8]==0)*A[,2:9]==1)
and adjust the 8 and the 9 for the number of columns in your matrix
Ben
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Guenther, Cameron
Sent: Tuesday, November 28, 2006 3:20 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Counting zeros in a matrix
Hi All,
If you could help me with this problem I would greatly appreciate it.
Suppose I have a matrix A:
1 1 1 1 0 1 1 1 1
1 1 1 0 1 0 1 0 0
1 0 1 0 0 1 0 0 0
1 1 0 0 0 0 1 0 0
I would like, for each row, to sum the number of times a 0 appears in
front of a 1. So what I would like is to have
Sum
1 1 1 1 0 1 1 1 1 1
1 1 1 0 1 0 1 0 0 2
1 0 1 0 0 1 0 0 0 2
1 1 0 0 0 0 1 0 0 1
I tried writing a function to do this but am getting mixed up in the
[i,j] coding. This is just an example the real matrix is much larger.
Thanks in advance.
Cameron Guenther, Ph.D.
100 8th Ave. SE
St. Petersburg, Fl 33701
727-896-8626 ext. 4305
cameron.guenther at myfwc.com
More information about the R-help
mailing list