Discarding a matrix based on the rowSums value
Hi, You write below:
What I specifically need is a way to write "If any of the elements of the SUMROW vector == N.1s then flag <-true else flag<-false".
And in fact, ?any is just what you need, as you said. any(SUMROW == N.1s) should do the trick. Sarah
On Mon, Nov 21, 2011 at 10:58 AM, Juan Antonio Balbuena <balbuena at uv.es> wrote:
Hello
I would appreciate your help on the followig. I want to generate random
binary matrices but I need to discard those with all-1 rows. That is, for a
10x10 matrix with five 1's
? ? ?[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
?[1,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 1
?[2,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?1 ? ?0 ? ?0 ? ? 0
?[3,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?1 ? ?0 ? ?0 ? ? 0
?[4,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[5,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[6,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[7,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[8,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[9,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
[10,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?1 ? ?1 ? ?0 ? ?0 ? ? 0
would be passed on to further computations, whereas
? ? ?[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
?[1,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0
?[2,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[3,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[4,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[5,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[6,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[7,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[8,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
?[9,] ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ?0 ? ? 0
[10,] ? ?0 ? ?1 ? ?0 ? ?1 ? ?0 ? ?1 ? ?1 ? ?0 ? ?0 ? ? 1
must be discarded.
For any N.1s <= Nrow, I am trying
Nrow = 10
Ncol = 10
N.1s = 5
flag <- TRUE
while (flag == TRUE) {
m <- matrix ((sample (c(rep(1, each=N.1s), rep(0,
each=((Ncol*Nrow)-N.1s))))), Nrow)
SUMROW <- rowSums(m)
? ? ? ?if (SUMROW == N.1s) {flag <- TRUE} else {flag <- FALSE}
? ? ? ?}
}
but I get this warning
Warning message:
In if (SUMROW == N.1s) { :
?the condition has length > 1 and only the first element will be used
What I specifically need is a way to write "If any of the elements of the
SUMROW vector == N.1s then flag <-true else flag<-false".
Any help would be much appreciated.
Sarah Goslee http://www.functionaldiversity.org