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
sample unique pairs from a matrix
7 messages · jos matejus, Richard Cotton, David Winsemius +1 more
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}}
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
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 <dwinsemius at 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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090528/9ed3c193/attachment-0001.pl>
Your last step will either be a single number (not really a sampling operation) or a non-positive number. So at best you really only have an number that depends entirely on the prior sequence of draws.
David.
On May 28, 2009, at 8:21 AM, jos matejus 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 <dwinsemius at 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
>>
>>
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
Dear All, Many thanks for all your useful suggestions. Much appreciated. Jos 2009/5/28 David Winsemius <dwinsemius at comcast.net>:
Your last step will either be a single number (not really a sampling operation) or a non-positive number. So at best you really only have an number that depends entirely on the prior sequence of draws. -- David. On May 28, 2009, at 8:21 AM, jos matejus 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 <dwinsemius at 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
David Winsemius, MD Heritage Laboratories West Hartford, CT