这是我的随机数据,我想订购它们,然后在不改变顺序的情况下拆分成相同的样本。我能够订购并分裂,但这是随机的,但是分成相同的样本,使得顺序保持不变是挑战。任何帮助都将受到高度赞赏。
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
以下代码执行订单并拆分但不维护订单。 示例:如果我将其分为5个相等的部分,它应该是这样的 (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)
答案 0 :(得分:0)
acct_details2 <- acct_details[order(acct_details$Probability),] #orders the probability
split_data <- split(acct_details2, rep(1:5, each = 2)) # thanks to Gregor and hpesoj
$`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