This is one way of sampling all the values in the matrix that meet your
criteria:

> set.seed(1)
> (x <- matrix(rnorm(100), 10))
            [,1]        [,2]        [,3]        [,4]       [,5]
[,6]        [,7]         [,8]       [,9]      [,10]
 [1,] -0.6264538  1.51178117  0.91897737  1.35867955 -0.1645236  0.3981059
2.40161776  0.475509529 -0.5686687 -0.5425200
 [2,]  0.1836433  0.38984324  0.78213630 -0.10278773 -0.2533617 -0.6120264
-0.03924000 -0.709946431 -0.1351786  1.2078678
 [3,] -0.8356286 -0.62124058  0.07456498  0.38767161  0.6969634  0.3411197
0.68973936  0.610726353  1.1780870  1.1604026
 [4,]  1.5952808 -2.21469989 -1.98935170 -0.05380504  0.5566632 -1.1293631
0.02800216 -0.934097632 -1.5235668  0.7002136
 [5,]  0.3295078  1.12493092  0.61982575 -1.37705956 -0.6887557  1.4330237
-0.74327321 -1.253633400  0.5939462  1.5868335
 [6,] -0.8204684 -0.04493361 -0.05612874 -0.41499456 -0.7074952  1.9803999
0.18879230  0.291446236  0.3329504  0.5584864
 [7,]  0.4874291 -0.01619026 -0.15579551 -0.39428995  0.3645820 -0.3672215
-1.80495863 -0.443291873  1.0630998 -1.2765922
 [8,]  0.7383247  0.94383621 -1.47075238 -0.05931340  0.7685329 -1.0441346
1.46555486  0.001105352 -0.3041839 -0.5732654
 [9,]  0.5757814  0.82122120 -0.47815006  1.10002537 -0.1123462  0.5697196
0.15325334  0.074341324  0.3700188 -1.2246126
[10,] -0.3053884  0.59390132  0.41794156  0.76317575  0.8811077 -0.1350546
2.17261167 -0.589520946  0.2670988 -0.4734006
> # create new matrix with R/C indicator
> x.ind <- cbind(R=as.vector(row(x)), C=as.vector(col(x)), val=as.vector(x))
> # only keep positive values
> x.ind <- x.ind[x.ind[, 'val'] >= 0,, drop=FALSE]
> # now loop and select a value then delete others in the same R/C
> while (nrow(x.ind) > 0){
+     i <- sample(nrow(x.ind), 1)
+     cat("sample:", x.ind[i,], "\n")
+     # remove matching R/C
+     x.ind <- x.ind[!((x.ind[, "R"] == x.ind[i, "R"]) | (x.ind[, "C"] ==
x.ind[i, "C"])),, drop=FALSE]
+ }
sample: 3 3 0.07456498
sample: 8 2 0.9438362
sample: 4 7 0.02800216
sample: 10 4 0.7631757
sample: 9 1 0.5757814
sample: 5 9 0.5939462
sample: 1 8 0.4755095
sample: 7 5 0.3645820
sample: 6 6 1.9804
sample: 2 10 1.207868
>


On Thu, May 28, 2009 at 8:21 AM, jos matejus <matejus...@googlemail.com>wrote:

> Dear Ritchie and David,
>
> Thanks very much for your advice. I had thought of this potential
> solution, however it doesn't really fullfill my second criteria which
> is that once a particular cell has been sampled, the row and column of
> that cell can't be sampled from subsequently. In other words, the next
> sample would be taken from a 5x5 matrix, and then a 4x4 matrix and so
> on until I have my 6 values.
>
> I will keep thinking!
> Cheers
> Jos
>
> 2009/5/28 David Winsemius <dwinsem...@comcast.net>:
>  >
> > On May 28, 2009, at 6:33 AM, jos matejus wrote:
> >
> >> Dear R users,
> >>
> >> 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
> >> Any help would be gratefully received.
> >> Jos
> >
> >> M <- matrix(rnorm(36),nrow=6)
> >
> >> M[M[,]>0]
> >  [1] 1.65619781 0.56182830 0.23812890 0.81493915 1.01279243 1.29188874
> > 0.64252343 0.53748655 0.31503112
> > [10] 0.37245358 0.07942883 0.56834586 0.62200056 0.39478167 0.02374574
> > 0.04974857 0.56219171 0.52901658
> >
> >
> >> sample(M[M[,]>0],6,replace=F)
> > [1] 0.56834586 0.07942883 0.31503112 0.62200056 0.02374574 0.64252343
> >
> > --
> > David Winsemius, MD
> > Heritage Laboratories
> > West Hartford, CT
> >
> >
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to