[R] the matrix of rows with specific row sums

Hong Ooi Hong.Ooi at iag.com.au
Wed Nov 23 00:14:51 CET 2005


_______________________________________________________________________________________


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




More information about the R-help mailing list