Sugand Anand
Sugand Anand

Reputation: 69

How to do non random sampling

This is the random data I have and I want to order them and then split into equal samples without changing the order. I'm able to order and also split but that is random but splitting into equal samples such that the order remains the same is the challenge. Any help would be highly appreciated.

acct_num <- c('4525','52222','22892','67181','67733','737382',
              '5534','2228','7282','45622')
Probability <- runif(10, min = 0, max = 10)
acct_details <- data.frame(acct_num, Probability)

acct_details

acct_num    Probability
4525    5.7406891
52222   0.8903079
22892   6.4435008
67181   2.2208483
67733   9.0374168
737382  0.8676233
5534    1.8472735
2228    8.4051774
7282    1.4664209
45622   1.8274586

The following code does the order and split but doesn't maintain the order. Example: If I divide it into 5 equal parts it should be like (1,2)(3,4),(5,6)(7,8),(9,10)

stopifnot(nrow(acct_details2) %% 5 == 0)
acct_details2    <- acct_details2[order(runif(nrow(acct_details2))),]
bins  <- rep(1:5, nrow(acct_details2) / 5)
split(acct_details2, bins)

Upvotes: 0

Views: 975

Answers (1)

Sugand Anand
Sugand Anand

Reputation: 69

acct_details2 <- acct_details[order(acct_details$Probability),] #orders the probability

splits the data equally retaining the order

split_data <- split(acct_details2, rep(1:5, each = 2)) # thanks to Gregor and hpesoj

the output with probability in order

$`1`
acct_num    Probability
6   737382  0.9741298
1   4525    1.5790106
$`2`
acct_num    Probability
8   2228    2.140016
7   5534    2.849498
$`3`
acct_num    Probability
9   7282    6.134280
3   22892   6.375977
$`4`
acct_num    Probability
2   52222   7.101398
10  45622   7.787715
$`5`
acct_num    Probability
4   67181   8.928878
5   67733   9.610741

Upvotes: 1

Related Questions