這個問題與這個問題非常相似,但不是將每個原始組中的一個專案放入每個輸出組中,而是希望每個輸出組中每個原始組中的多個專案。
我有以下資料。
list1 <- list(Group_1 = c("1", "2", "3", "4", "5", "6"), Group_2 = c("13", "14", "15", "16", "17", "18"), Group_3 = c("19", "20", "21", "22", "23", "24", "25"))
Number_of_Items_From_Each_Original_Group_to_End_up_in_Each_Output_Group <- 2
Group_1
包含 6 項,Group_2
包含 6 項,Group_3
包含 7 項。基于這些初始組,我想將專案放入 3 個新組中,確保每個新組包含Number_of_Items_From_Each_Original_Group_to_End_up_in_Each_Output_Group
每個原始組中相同數量(2 或 )的專案。此外,這 3 個新組中的任何一個專案都不能重復 - 每個專案只能在每個新組中使用一次。例如,一個可能的輸出可能是下面的這個輸出。
(list(New_Group_1 = c("1", "2", "13", "14", "19", "20"), New_Group_2 = c("3", "4", "15", "16", "21", "22"), New_Group_3 = c("5", "6", "17", "18", "23", "24")))
我實際上想生成一個list
顯示每個可能輸出(每個可能的組合)的輸出。對于每個串列元素,應該有三個新組,每個組中應該有不同的專案。例如,除了上面的解決方案之外,另一個可能是以下解決方案。
(list(New_Group_1 = c("3", "4", "13", "14", "19", "20"), New_Group_2 = c("5", "6", "15", "16", "21", "22"), New_Group_3 = c("1", "2", "17", "18", "23", "24")))
我在這里問的問題非常相似,只是在那個問題中,我只將每個原始組中的一項放入每個新組中。在這里,我想選擇將每個原始組中的多個專案放入每個新組中。
uj5u.com熱心網友回復:
這是相當棘手的,而且相當緩慢,因為指定的三個組有 23,625 種可能的組合
n <- 2
all_pairs <- lapply(list1, function(group) {
all_combs <- apply(combn(ncol(combn(length(group), n)), length(list1)),
2,
function(x) c(combn(length(group), n)[,x]), simplify = FALSE)
all_combs[sapply(all_combs, function(x) !any(duplicated(x)))]
})
index_df <- do.call(expand.grid, lapply(all_pairs, function(x) seq(length(x))))
result <- apply(index_df, 1, function(x) {
m <- t(sapply(seq_along(x), function(y) list1[[y]][all_pairs[[y]][[x[y]]]]))
setNames(lapply(split(m, (seq_along(m)-1) %/% (length(list1) * n)),
function(x) c(t(matrix(x, ncol = n)))), names(list1))
}, simplify = FALSE)
結果串列如下所示:
head(result)
#> [[1]]
#> [[1]]$Group_1
#> [1] "1" "2" "13" "14" "19" "20"
#>
#> [[1]]$Group_2
#> [1] "3" "4" "15" "16" "21" "22"
#>
#> [[1]]$Group_3
#> [1] "5" "6" "17" "18" "23" "24"
#>
#>
#> [[2]]
#> [[2]]$Group_1
#> [1] "1" "2" "13" "14" "19" "20"
#>
#> [[2]]$Group_2
#> [1] "3" "5" "15" "16" "21" "22"
#>
#> [[2]]$Group_3
#> [1] "4" "6" "17" "18" "23" "24"
#>
#>
#> [[3]]
#> [[3]]$Group_1
#> [1] "1" "2" "13" "14" "19" "20"
#>
#> [[3]]$Group_2
#> [1] "3" "6" "15" "16" "21" "22"
#>
#> [[3]]$Group_3
#> [1] "4" "5" "17" "18" "23" "24"
#>
#>
#> [[4]]
#> [[4]]$Group_1
#> [1] "1" "3" "13" "14" "19" "20"
#>
#> [[4]]$Group_2
#> [1] "2" "4" "15" "16" "21" "22"
#>
#> [[4]]$Group_3
#> [1] "5" "6" "17" "18" "23" "24"
#>
#>
#> [[5]]
#> [[5]]$Group_1
#> [1] "1" "3" "13" "14" "19" "20"
#>
#> [[5]]$Group_2
#> [1] "2" "5" "15" "16" "21" "22"
#>
#> [[5]]$Group_3
#> [1] "4" "6" "17" "18" "23" "24"
#>
#>
#> [[6]]
#> [[6]]$Group_1
#> [1] "1" "3" "13" "14" "19" "20"
#>
#> [[6]]$Group_2
#> [1] "2" "6" "15" "16" "21" "22"
#>
#> [[6]]$Group_3
#> [1] "4" "5" "17" "18" "23" "24"
由reprex 包(v2.0.1)創建于 2022-06-09
uj5u.com熱心網友回復:
可能你可以定義一個像下面這樣的函式(它生成按元素f
分組的所有排列)n
v
f <- function(v, n = 2) {
if (length(v) < n) {
return(list(NULL))
}
if (length(v) == n) {
return(list(v))
}
x <- combn(v, n, simplify = FALSE)
unlist(lapply(x, function(p) Map(rbind, list(p), f(v[!v %in% p], n))), recursive = FALSE)
}
res <- apply(expand.grid(lapply(list1, f)), 1, function(x) asplit(do.call(cbind, x), 1))
結果的一個子集看起來像
> apply(head(expand.grid(lapply(list1, f))), 1, fun .... [TRUNCATED]
$`1`
$`1`[[1]]
[1] "1" "2" "13" "14" "19" "20"
$`1`[[2]]
[1] "3" "4" "15" "16" "21" "22"
$`1`[[3]]
[1] "5" "6" "17" "18" "23" "24"
$`2`
$`2`[[1]]
[1] "1" "2" "13" "14" "19" "20"
$`2`[[2]]
[1] "3" "5" "15" "16" "21" "22"
$`2`[[3]]
[1] "4" "6" "17" "18" "23" "24"
$`3`
$`3`[[1]]
[1] "1" "2" "13" "14" "19" "20"
$`3`[[2]]
[1] "3" "6" "15" "16" "21" "22"
$`3`[[3]]
[1] "4" "5" "17" "18" "23" "24"
$`4`
$`4`[[1]]
[1] "1" "2" "13" "14" "19" "20"
$`4`[[2]]
[1] "4" "5" "15" "16" "21" "22"
$`4`[[3]]
[1] "3" "6" "17" "18" "23" "24"
$`5`
$`5`[[1]]
[1] "1" "2" "13" "14" "19" "20"
$`5`[[2]]
[1] "4" "6" "15" "16" "21" "22"
$`5`[[3]]
[1] "3" "5" "17" "18" "23" "24"
$`6`
$`6`[[1]]
[1] "1" "2" "13" "14" "19" "20"
$`6`[[2]]
[1] "5" "6" "15" "16" "21" "22"
$`6`[[3]]
[1] "3" "4" "17" "18" "23" "24"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/488553.html
下一篇:無法使用正則運算式洗掉R中的符號