Sample pairs to satisfy a condition

Open your eyes

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?


Edited:

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.

Open your eyes

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!!

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Select columns that satisfy a condition

分類Dev

How to sample on condition with pandas?

分類Dev

How can I fill a matrix with random values that need to satisfy a condition?

分類Dev

All the subsets of size N which satisfy a condition in Haskell

分類Dev

Detect condition between pairs of entries on a Python list

分類Dev

awk command to sum pairs of lines and filter out under particular condition

分類Dev

Output row index of first element of every column in a matrix to satisfy a logical condition

分類Dev

Why does my else statement in prolog execute even if it doesn't satisfy the condition?

分類Dev

How to query findOne() in mongoose such that we get subset of array of documents which satisfy particular condition?

分類Dev

How can I get the records based on condition on date and time of created_time column of sample dataframe

分類Dev

Javascript: Array of dictionaries, get all key value pairs from one dictionairy with condition

分類Dev

All pairs of pairs python

分類Dev

JDBC - Convert SQL string to satisfy Java

分類Dev

Build association while initializing to satisfy delegates

分類Dev

Get a random sample of a dict

分類Dev

Spark sample is too slow

分類Dev

Oversampling with sample function

分類Dev

Fast sorted sample with replacement

分類Dev

IterativeImputer-sample_posterior

分類Dev

Sample data for an ADC

分類Dev

Metaprogramming sample from Wikipedia

分類Dev

A sample UML Activity Diagram

分類Dev

Binomial Sample Size with r

分類Dev

Traverse an array of pairs

分類Dev

find duplicates in pairs in mysql

分類Dev

Consecutive pairs with recursion on haskell

分類Dev

Finding unique pairs in array

分類Dev

Sort pairs to be more consecutive

分類Dev

Identifying keyword pairs in lex

Related 関連記事

  1. 1

    Select columns that satisfy a condition

  2. 2

    How to sample on condition with pandas?

  3. 3

    How can I fill a matrix with random values that need to satisfy a condition?

  4. 4

    All the subsets of size N which satisfy a condition in Haskell

  5. 5

    Detect condition between pairs of entries on a Python list

  6. 6

    awk command to sum pairs of lines and filter out under particular condition

  7. 7

    Output row index of first element of every column in a matrix to satisfy a logical condition

  8. 8

    Why does my else statement in prolog execute even if it doesn't satisfy the condition?

  9. 9

    How to query findOne() in mongoose such that we get subset of array of documents which satisfy particular condition?

  10. 10

    How can I get the records based on condition on date and time of created_time column of sample dataframe

  11. 11

    Javascript: Array of dictionaries, get all key value pairs from one dictionairy with condition

  12. 12

    All pairs of pairs python

  13. 13

    JDBC - Convert SQL string to satisfy Java

  14. 14

    Build association while initializing to satisfy delegates

  15. 15

    Get a random sample of a dict

  16. 16

    Spark sample is too slow

  17. 17

    Oversampling with sample function

  18. 18

    Fast sorted sample with replacement

  19. 19

    IterativeImputer-sample_posterior

  20. 20

    Sample data for an ADC

  21. 21

    Metaprogramming sample from Wikipedia

  22. 22

    A sample UML Activity Diagram

  23. 23

    Binomial Sample Size with r

  24. 24

    Traverse an array of pairs

  25. 25

    find duplicates in pairs in mysql

  26. 26

    Consecutive pairs with recursion on haskell

  27. 27

    Finding unique pairs in array

  28. 28

    Sort pairs to be more consecutive

  29. 29

    Identifying keyword pairs in lex

ホットタグ

アーカイブ