[R] Discarding a matrix based on the rowSums value

R. Michael Weylandt michael.weylandt at gmail.com
Mon Nov 21 18:04:53 CET 2011


any()

Michael

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.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Discarding-a-matrix-based-on-the-rowSums-value-tp4091995p4091995.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



More information about the R-help mailing list