gpt4 book ai didi

固定长度的随机子集,使得每个组至少出现 N 次

转载 作者:行者123 更新时间:2023-12-02 01:08:53 25 4
gpt4 key购买 nike

我想从 df 的 column1 中的每个值中选择 5 行,以便输出对于 column2 中的每个唯一值至少有 1 个值。输出中也不应该有任何重复

编辑:(column1, column3) 对中不应有重复项:即对于 column1 中的每个值,column3 中的所有值都应该是唯一的

column1 = rep(c("a","b"), each = 12)
column2 = rep(c(1,2,3), each = 4)
column3 = c("x1","x2","x3","x4","x5","x3","x6","x7","x8","x1","x9","x5","x6","x2","x3","x4","x7","x5","x6","x1","x4","x1","x6","x9")

df = data.frame(column1, column2, column3)

这是一个有效的解决方案

sample_output_1 = data.frame(column1 = rep(c("a","b"), each = 5),
column2 = c(1,1,2,2,3,1,1,2,2,3),
column3 = c("x1","x2","x5","x3","x8","x6","x2","x5","x1","x9"))

最佳答案

检查这个

foo = function(a_df){
inds = 1:NROW(a_df)
#Sample 5 indices along the rows of a_df
my_inds = sample(inds, 5)
#If subset of a_df based on my_inds has duplicates
#Or if 2nd column does not have all unique values
while(any(duplicated(a_df[my_inds, c(1, 3)])) &
!identical(sort(unique(a_df[my_inds, 2])), sort(unique(a_df[[2]])))){
#Count the number of duplicates or missing all values
n = sum(duplicated(a_df[my_inds, c(1, 3)]))
n = n + sum(!sort(unique(a_df[my_inds, 2])) %in% sort(unique(a_df[[2]])))
#Remove my_inds from inds
inds = inds[!inds %in% my_inds]
#Remove the n indices that create duplicates from my_nds
my_inds = my_inds[!duplicated(a_df[my_inds, c(1, 3)])]
#Sample n more from inds and add to my_inds
my_inds = sample(c(my_inds, sample(inds, n)))
}
return(a_df[my_inds,])
}

set.seed(42)
do.call(rbind, lapply(split(df, df$column1), function(a) foo(a_df = a)))
# column1 column2 column3
# a.11 a 3 x9
# a.12 a 3 x5
# a.3 a 1 x3
# a.8 a 2 x7
# a.6 a 2 x3
# b.19 b 2 x6
# b.21 b 3 x4
# b.14 b 1 x2
# b.18 b 2 x5
# b.23 b 3 x6

关于固定长度的随机子集,使得每个组至少出现 N 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46123708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com