Juan Antonio Balbuena
2011-Nov-21 15:58 UTC
[R] Discarding a matrix based on the rowSums value
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. Michael Weylandt
2011-Nov-21 17:04 UTC
[R] Discarding a matrix based on the rowSums value
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.
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
On Nov 21, 2011, at 10:58 AM, Juan Antonio Balbuena 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.Why? it has no rows that meet your verbal description " all-1 rows" (Nor it would meet your code implementation below, but I offer a suggestion in case you want only to exclude matrices with exactly rows whose sum is 5 but would accept those with sums of 4, or 6, etc.)> > 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}rowSums retruns a vector, whereas if() only works with vectors of length 1. Perhaps you want: flag <- ifelse(SUMROW == N.1s, TRUE, 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.David Winsemius, MD West Hartford, CT
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Nov-23 14:27 UTC
[R] Discarding a matrix based on the rowSums value
If it's working you are just getting lucky: this is the syntax you want/need: if(any(rowSums(m) == N.1s)) flag <- TRUE You test each of the rowSums and then you check if any of the results are true. That warning came up because R has a way to convert numbers to T/F values but it hesitates because it's a weird thing to do. Michael On Nov 23, 2011, at 5:55 AM, Juan Antonio Balbuena <j.a.balbuena@uv.es> wrote:> Hello > Thank you very much for your help. > > I eventually tried > > if(any(rowSums(m)) == N.1s) flag <- TRUE > > and seems to do what I intended. > > Although a get a warning: > > In any(rowSums(m)) : coercing argument of type 'double' to logical > > It seems to be harmless. > > Thank you very much again for your time. > > Juan A. Balbuena > > > > El 21/11/2011 18:04, R. Michael Weylandt escribió: >> >> any() >> >> Michael >> >> On Mon, Nov 21, 2011 at 10:58 AM, Juan Antonio Balbuena <balbuena@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@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. >> > > -- > Dr. Juan A. Balbuena > Marine Zoology Unit > Cavanilles Institute of Biodiversity and Evolutionary Biology > University of Valencia http://www.uv.es/~balbuena > P.O. Box 22085 http://www.uv.es/cavanilles/zoomarin/index.htm > 46071 Valencia, Spain http://cetus.uv.es/mullpardb/index.html > e-mail: j.a.balbuena@uv.es tel. +34 963 543 658 fax +34 963 543 733 > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > NOTE! For shipments by EXPRESS COURIER use the following street address: > C/ Catedrático José Beltrán 2, 46980 Paterna (Valencia), Spain. > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[[alternative HTML version deleted]]