[R] the matrix of rows with specific row sums

Seth Falcon sfalcon at fhcrc.org
Tue Nov 22 22:37:34 CET 2005


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




More information about the R-help mailing list