[R] How to subset a matrix?
Petr Savicky
savicky at cs.cas.cz
Tue Jun 12 19:19:38 CEST 2012
On Tue, Jun 12, 2012 at 11:44:51AM -0500, Michael wrote:
> Hi all,
>
> Lets say I have a matrix A which is m x n.
>
> I also have a mask matrix MASK which is m x n with values in T/F, where T
> values make a sub-matrix in regutangular shape...
>
> I applied B=A[MASK] and it didn't work as expected...
Hi.
Try the following.
A <- matrix(1:16, nrow=4, ncol=4)
B <- matrix(FALSE, nrow=4, ncol=4)
B[2:3, 2:3] <- TRUE
A[rowSums(B) != 0, colSums(B) != 0]
[,1] [,2]
[1,] 6 10
[2,] 7 11
Hope this helps.
Petr Savicky.
More information about the R-help
mailing list