使用R中的过滤器随机化项目

cgvd09ve  于 2023-03-10  发布在  其他
关注(0)|答案(2)|浏览(118)

有没有一种方法可以根据过滤器来打乱 Dataframe 的行?例如,我有这样一个 Dataframe :

data=data.frame(id=c(3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26),
                name=c("restructuring","restructuring","restructuring","restructuring",
                       "control","control","control","control","clitic filler","clitic filler","clitic filler","clitic filler","clitic filler","clitic filler","clitic filler","clitic filler","action filler","action filler","action filler","action filler","action filler","action filler","action filler","action filler")
               )

其中数字3到6是“重构”,7-10是“控制”,11-18是“cltic填充器”,19-26是“操作填充器”,我希望name列在2个连续行中不具有相同的值。
我试过:
shuffled_data= data[sample(1:nrow(data)), ]
但这显然是随机的,没有标准

wrrgggsh

wrrgggsh1#

如果您的数据大约是这个大小,我会做一堆随机 Shuffle ,并找到一个(s)符合您的标准:

shuffle = function(data) {
  data[sample(1:nrow(data)), ]
}

check = function(data) {
  all(data$name[-1] != data$name[-nrow(data)])
}

set.seed(47)
results = replicate(10000, shuffle(data), simplify = FALSE)
results = results[sapply(results, check)]
length(results)
[1] 10
## 10 of the 10000 shuffles meet your criteria

## here's one:
results[[1]]
#    id          name
# 16 18 clitic filler
# 21 23 action filler
# 9  11 clitic filler
# 20 22 action filler
# 15 17 clitic filler
# 24 26 action filler
# 1   3 restructuring
# 13 15 clitic filler
# 7   9       control
# 2   4 restructuring
# 19 21 action filler
# 6   8       control
# 4   6 restructuring
# 23 25 action filler
# 3   5 restructuring
# 22 24 action filler
# 10 12 clitic filler
# 18 20 action filler
# 12 14 clitic filler
# 5   7       control
# 11 13 clitic filler
# 8  10       control
# 17 19 action filler
# 14 16 clitic filler
3j86kqsm

3j86kqsm2#

this answer中的函数用于min.dist = 1

library(data.table)

setorder(setDT(data), name)[
  frank(prob_shuffler(cumsum(!duplicated(name)), 1L), ties.method = "random")
]
#>     id          name
#>  1:  3 restructuring
#>  2: 10       control
#>  3: 17 clitic filler
#>  4:  7       control
#>  5:  5 restructuring
#>  6: 13 clitic filler
#>  7: 20 action filler
#>  8: 11 clitic filler
#>  9:  9       control
#> 10: 24 action filler
#> 11: 16 clitic filler
#> 12: 25 action filler
#> 13: 14 clitic filler
#> 14: 19 action filler
#> 15:  4 restructuring
#> 16: 18 clitic filler
#> 17: 22 action filler
#> 18:  6 restructuring
#> 19: 23 action filler
#> 20: 15 clitic filler
#> 21:  8       control
#> 22: 21 action filler
#> 23: 12 clitic filler
#> 24: 26 action filler

相关问题