[R] Randomly remove condition-selected rows from a matrix

Stavros Macrakis macrakis at alum.mit.edu
Tue Dec 30 18:20:27 CET 2008


I believe this does what you want:

m[-sample(which(m[,1]<8 & m[,2]>12),2),]

Analysis:

Get a boolean vector of rows fitting criteria:
    m[,1]<8 & m[,2]>12

What are their indexes?
    which(...)

Choose two among those indexes:
     sample(...,2)

Choose all except the selected rows from the original:
     m[- ... , ]

          -s

On Tue, Dec 30, 2008 at 9:59 AM, Guillaume Chapron
<carnivorescience at gmail.com> wrote:

> m <- matrix(1:20, nrow = 10, ncol = 2)
>         [,1] [,2]
>  [1,]    1   11
>  [2,]    2   12
>  [3,]    3   13
>  [4,]    4   14
>  [5,]    5   15
>  [6,]    6   16
>  [7,]    7   17
>  [8,]    8   18
>  [9,]    9   19
> [10,]   10   20
>
> Then, I want to remove randomly 2 rows among the ones where m[,1]<8 and
> m[,2]>12



More information about the R-help mailing list