Re: [R] Counting zeros in a matrix

2006-11-28 Thread Ben Fairbank
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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Guenther, Cameron
Sent: Tuesday, November 28, 2006 3:20 PM
To: [email protected]
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
[EMAIL PROTECTED]

__
[email protected] 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.


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 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
> [EMAIL PROTECTED]
> __
> [email protected] 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.

__
[email protected] 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.


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))

__
[email protected] 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.


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 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.

Not fully tested, but how about this:

> mat
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
1111101111
2111010100
3101001000
4110000100


> cbind(mat, colSums(apply(mat, 1, diff) == 1))
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
1111101111 1
2111010100 2
3101001000 2
4110000100 1

HTH,

Marc Schwartz

__
[email protected] 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.


Re: [R] Counting zeros in a matrix

2006-11-28 Thread Leeds, Mark \(IED\)
temp<-apply(A,1,sum(diff(x)) == -1 ) 


but check it because I haven't tested it and when things are not tested
there
can often be unforeseen problems. Also, may have to declare sum(diff(x))
== -1 as a separate function and then call it.
I'm not sure aobut that.






-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Guenther, Cameron
Sent: Tuesday, November 28, 2006 4:20 PM
To: [email protected]
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
[EMAIL PROTECTED]


This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}

__
[email protected] 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.