[R] Counting zeros in a matrix

2006-11-28 Thread Guenther, Cameron
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

Re: [R] Counting zeros in a matrix

2006-11-28 Thread Leeds, Mark \(IED\)
- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Guenther, Cameron Sent: Tuesday, November 28, 2006 4:20 PM To: r-help@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

Re: [R] Counting zeros in a matrix

2006-11-28 Thread Marc Schwartz
On Tue, 2006-11-28 at 16:20 -0500, Guenther, Cameron wrote: 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

Re: [R] Counting zeros in a matrix

2006-11-28 Thread Richard M. Heiberger
A - matrix(c( 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), 4, 9, byrow=TRUE) apply(A, 1, function(x) sum(diff(x)==1)) __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read

Re: [R] Counting zeros in a matrix

2006-11-28 Thread Benilton Carvalho
countZerosBeforeOnes - function(v) sum(v[1:max(which(v == 1))] == 0) apply(A, 1, countZerosBeforeOnes) cheers, b On Nov 28, 2006, at 4:20 PM, Guenther, Cameron wrote: 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

Re: [R] Counting zeros in a matrix

2006-11-28 Thread Ben Fairbank
] 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