Message-ID: <OF0A75AAD8.11D314D0-ON802575C4.003C14B3-802575C4.003C461F@hsl.gov.uk>
Date: 2009-05-28T10:58:17Z
From: Richard Cotton
Subject: sample unique pairs from a matrix
In-Reply-To: <d003a00f0905280333j29b05f7wef339c2580c95250@mail.gmail.com>
> I have a matrix of both negative and positive values that I would like
> to randomly sample with the following 2 conditions:
>
> 1. only sample positive values
> 2. once a cell in the matrix has been sampled the row and column of
> that cell cannot be sampled from again.
>
> #some dummy data
> set.seed(101)
> dataf <- matrix(rnorm(36,1,2), nrow=6)
>
> I can do this quite simply if all the values are positive by using the
> sample function without replacement on the column and row indices.
>
> samrow <- sample(6,replace=F)
> samcol <- sample(6,replace=F)
> values <- numeric(6)
> for(i in 1:6){
> values[i] <- dataf[samrow[i], samcol[i]]
> }
>
> However, I am not sure how to include the logical condition to only
> include postitive values
You could create a new variable containing only the positive values of
dataf, and sample from that, e.g.
dataf <- matrix(rnorm(36,1,2), nrow=6)
posdata <- dataf[dataf > 0]
sample(posdata, 6, replace=FALSE)
Regards,
Richie.
Mathematical Sciences Unit
HSL
------------------------------------------------------------------------
ATTENTION:
This message contains privileged and confidential inform...{{dropped:20}}