I am just starting with R and have the following problem: given a matrix of ones and zeroes, say mdim=4 m<-matrix(round(runif(mdim^mdim)),mdim,mdim) how to construct the matrix of those rows of m, whose elements sum to 2. Contrary to the random matrix above, the actual matrix always has at least one such row. Serguei Kaniovski
On Tue, 2005-11-22 at 21:55 +0100, Serguei Kaniovski wrote:> mdim=4 > m<-matrix(round(runif(mdim^mdim)),mdim,mdim)One approach:> m[,1] [,2] [,3] [,4] [1,] 0 0 1 0 [2,] 0 1 1 0 [3,] 0 0 1 1 [4,] 0 0 0 0> idx <- apply(m, 1, sum) > m[idx,][,1] [,2] [,3] [,4] [1,] 0 0 1 0 [2,] 0 1 1 0 [3,] 0 1 1 0>------------------------------------------------------------------- Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE ------------------------------------------------------------------- Cogito cogito ergo cogito sum (I think that I think, therefore I think that I am.) -- Ambrose Bierce
On 22 Nov 2005, kaniovsk at wsr.ac.at wrote:> I am just starting with R and have the following problem: given a > matrix of ones and zeroes, say > > mdim=4 > m<-matrix(round(runif(mdim^mdim)),mdim,mdim) > > how to construct the matrix of those rows of m, whose elements sum > to 2. Contrary to the random matrix above, the actual matrix always > has at least one such row.Untested, but I think you want something like: idx <- apply(m, 1, sum) == 2 ans <- m[idx, ] + seth
Standard caution: == should not be used for comparison of floating point numbers. See ?all.equal and ?identical. See also the Green Book (Chambers: PROGRAMMING WITH DATA). Sometimes what you think are integers may be floats, too, as numerous postings to this list have shown. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Seth Falcon > Sent: Tuesday, November 22, 2005 1:38 PM > To: r-help at stat.math.ethz.ch > Subject: Re: [R] the matrix of rows with specific row sums > > On 22 Nov 2005, kaniovsk at wsr.ac.at wrote: > > > I am just starting with R and have the following problem: given a > > matrix of ones and zeroes, say > > > > mdim=4 > > m<-matrix(round(runif(mdim^mdim)),mdim,mdim) > > > > how to construct the matrix of those rows of m, whose elements sum > > to 2. Contrary to the random matrix above, the actual matrix always > > has at least one such row. > > Untested, but I think you want something like: > > idx <- apply(m, 1, sum) == 2 > ans <- m[idx, ] > > > + seth > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
_______________________________________________________________________________________ There is also rowSums (and colSums) which does the same thing without needing apply: m[rowSums(m) == 2, ] Subject to Bert Gunter's caveat about comparing floating point numbers for equality, of course. -- Hong Ooi Senior Research Analyst, IAG Limited 388 George St, Sydney NSW 2000 (02) 9292 1566 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Seth Falcon Sent: Wednesday, 23 November 2005 8:38 AM To: r-help at stat.math.ethz.ch Subject: Re: [R] the matrix of rows with specific row sums On 22 Nov 2005, kaniovsk at wsr.ac.at wrote:> I am just starting with R and have the following problem: given a > matrix of ones and zeroes, say > > mdim=4 > m<-matrix(round(runif(mdim^mdim)),mdim,mdim) > > how to construct the matrix of those rows of m, whose elements sum > to 2. Contrary to the random matrix above, the actual matrix always > has at least one such row.Untested, but I think you want something like: idx <- apply(m, 1, sum) == 2 ans <- m[idx, ] + seth ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html _______________________________________________________________________________________ The information transmitted in this message and its attachme...{{dropped}}