I have this problem I cannot figure out. I have 500 samples of Group A from Uniform distribution. And there are 500 samples of Group B from another Uniform distribution.
I will select one value, a from A, and another value,b from B. I want to make 'a is always smaller than b'. I would like to get 500 pairs without duplication.
A <- runif(500, min = 19, max= 23)
B <- runif(500, min = 22, max= 26)
How can I get 500 pairs of (a,b) which are a < b, without duplication?
Sorry, I need to make clear my question. Once Group A and B is set, it will not be changed. 500 of pairs should be selected from fixed A and B. In the each pair, a < b.
I want to see 'random' effect like Monte Carlo. so, I think just sorting cannot help this problem.
This is not the prettiest solution, too. Anyway, I solved it! I used sample function with a condition and replaced a selected value with NA to prevent the duplication.
A <- runif(500, min = 19, max= 23)
B <- runif(500, min = 22, max= 26)
B.largerthan.A <- function(A,B) {
result = c()
i <- 1
while (i < 500) {
Select.B <- sample(B[!is.na(B)], size=1)
if ( (Select.B < max(A,na.rm=TRUE)) & (!is.na(Select.B)) ) {
Select.A <- sample((A)[(A<Select.B) & (!is.na(A))], size=1)
} else {
Select.A <- sample((A[!is.na(A)]),size=1)
}
result = rbind(result, c(Select.A, Select.B))
A[which(A == Select.A)] = NA
B[which(B == Select.B)] = NA
i=1+i
if (length(B[!is.na(B)]) == 1) {
Select.B <- B[!is.na(B)]
Select.A <- A[!is.na(A)]
result = rbind(result, c(Select.A, Select.B))
A[which(A == Select.A)] = NA
B[which(B == Select.B)] = NA
break
}}
return(result)
}
A_B <- B.largerthan.A(A,B)
It yields:
> any(A_B[,1] < A_B[,2])
[1] TRUE
If you have any tidier idea. Please let me know. THANK YOU!!