我有兩個向量:
group1 <- c("a", "b")
group2 <- c("c", "d", "e")
我想從“group1”中獲取一個元素的所有組合,從“group2”中獲取一個或兩個元素。
期望的結果是:
"a" "c" # one element from group1, one element from group 2
"b" "c"
"a" "d"
"b" "d"
"a" "e"
"b" "e"
"a" "c" "d" # one element from group1, two elements from group 2
"b" "c" "d"
"a" "c" "e"
"b" "c" "e"
"a" "d" "e"
"b" "d" "e"
uj5u.com熱心網友回復:
您可以像這樣獲得第二組的組合:
g = c(as.list(group2), combn(group2,2,simplify=F))
然后使用lapply()
這些,每次c()
與以下元素一起使用group1
:
unlist(lapply(g, \(i) lapply(group1,c,i)), recursive=F)
輸出:
[[1]]
[1] "a" "c"
[[2]]
[1] "b" "c"
[[3]]
[1] "a" "d"
[[4]]
[1] "b" "d"
[[5]]
[1] "a" "e"
[[6]]
[1] "b" "e"
[[7]]
[1] "a" "c" "d"
[[8]]
[1] "b" "c" "d"
[[9]]
[1] "a" "c" "e"
[[10]]
[1] "b" "c" "e"
[[11]]
[1] "a" "d" "e"
[[12]]
[1] "b" "d" "e"
uj5u.com熱心網友回復:
允許任何輸入向量以及從每個向量中提取的最大/最小字符數的完全通用解決方案是:
comb <- function(a, b, min_a = 1, max_a = 1, min_b = 1, max_b = 2) {
as <- do.call(c, lapply(min_a:max_a, \(i) combn(a, i, simplify = FALSE)))
bs <- do.call(c, lapply(min_b:max_b, \(i) combn(b, i, simplify = FALSE)))
apply(expand.grid(as, bs), 1, unlist, use.names = FALSE)
}
默認情況下,這將給出:
comb(group1, group2)
#> [[1]]
#> [1] "a" "c"
#>
#> [[2]]
#> [1] "b" "c"
#>
#> [[3]]
#> [1] "a" "d"
#>
#> [[4]]
#> [1] "b" "d"
#>
#> [[5]]
#> [1] "a" "e"
#>
#> [[6]]
#> [1] "b" "e"
#>
#> [[7]]
#> [1] "a" "c" "d"
#>
#> [[8]]
#> [1] "b" "c" "d"
#>
#> [[9]]
#> [1] "a" "c" "e"
#>
#> [[10]]
#> [1] "b" "c" "e"
#>
#> [[11]]
#> [1] "a" "d" "e"
#>
#> [[12]]
#> [1] "b" "d" "e"
使用reprex v2.0.2創建于 2022-09-03
uj5u.com熱心網友回復:
兩次呼叫expand.grid
將執行此操作:
eg1 <- expand.grid(group1, group2)
eg1
# Var1 Var2
# 1 a c
# 2 b c
# 3 a d
# 4 b d
# 5 a e
# 6 b e
和
eg2 <- expand.grid(group1, seq_len(ncol(combn(group2, 2))))
eg2 <- cbind(eg2, t(combn(group2, 2)[, eg2$Var2]))[,-2]
eg2
# Var1 1 2
# 1 a c d
# 2 b c d
# 3 a c e
# 4 b c e
# 5 a d e
# 6 b d e
然后它們可以與c
. 出于演示目的,我將在這里取消它們的名稱,盡管呼叫unname
純粹是裝飾性的。
out <- c(asplit(eg1, 1), asplit(eg2, 1))
out <- lapply(out, function(z) unname(c(z)))
str(out)
# List of 12
# $ : chr [1:2] "a" "c"
# $ : chr [1:2] "b" "c"
# $ : chr [1:2] "a" "d"
# $ : chr [1:2] "b" "d"
# $ : chr [1:2] "a" "e"
# $ : chr [1:2] "b" "e"
# $ : chr [1:3] "a" "c" "d"
# $ : chr [1:3] "b" "c" "d"
# $ : chr [1:3] "a" "c" "e"
# $ : chr [1:3] "b" "c" "e"
# $ : chr [1:3] "a" "d" "e"
# $ : chr [1:3] "b" "d" "e"
uj5u.com熱心網友回復:
1)此解決方案僅使用基礎 R 并且僅使用一次combn
. 它的作業原理是采用 3 個元素的所有組合,c(group1, group2, "")
并只保留 group1 中的一個元素和 group2 中的 1 或 2 個元素。"" 元素被洗掉,留下所需的向量串列。我們還展示了如何選擇性地將其表示為矩陣。
ok
如果出現inix
元素,則將函式定義為 TRUE,否則定義為FALSE。如果 group1 中有一個元素并且 group2 中有 1 或 2 個元素,則定義回傳其引數而不包含任何 "" 組件;否則,它回傳 NULL。適用于 的 3 個元素的所有組合。然后洗掉任何長度為零的行并回傳串列。如果您想要一個字符向量串列作為結果,請使用它。g
h
g
g
c(group1, group2, "")
L
如果需要矩陣結果而不是串列結果,則也使用最后一行。
ok <- function(g, h, ix) sum(g %in% h) %in% ix
g <- function(x) if (ok(x, group1, 1) && ok(x, group2, 1:2)) x[nchar(x) > 0]
L <- Filter(length, combn(c(group1, group2, ""), 3, g, simplify = FALSE))
# next line is only if you want a matrix result
do.call("rbind", lapply(L, `[`, 1:3))
給出這個矩陣:
[,1] [,2] [,3]
[1,] "a" "c" "d"
[2,] "a" "c" "e"
[3,] "a" "c" NA
[4,] "a" "d" "e"
[5,] "a" "d" NA
[6,] "a" "e" NA
[7,] "b" "c" "d"
[8,] "b" "c" "e"
[9,] "b" "c" NA
[10,] "b" "d" "e"
[11,] "b" "d" NA
[12,] "b" "e" NA
1a)這也可以用這樣的管道來表示。 g
是從上面。
c(group1, group2, "z") |>
combn(3, g, simplify = FALSE) |>
Filter(f = length) |>
lapply(`[`, 1:3) |> # this & next line if matrix wanted
do.call(what = "rbind")
2)使用字串操作的方法是將組合輸出的每個組件粘貼在一起,然后使用 str_count 對其進行過濾以計算出現次數。這給出了一個字符向量結果,或者我們可以使用將其拆分為一個串列strsplit(res, "")
。
library(stringr)
cc <- combn(c(group1, group2, ""), 3, paste, collapse = "")
res <- cc[str_count(cc, "[ab]") == 1 & str_count(cc, "[cde]") %in% 1:2]
res
## [1] "acd" "ace" "ac" "ade" "ad" "ae" "bcd" "bce" "bc" "bde" "bd" "be"
uj5u.com熱心網友回復:
我們可以用
a <- expand.grid(group1 , group2 , stringsAsFactors = F)
a$Var3 -> ""
b <- expand.grid(group1 , group2[-3] , group2[-1] ,
stringsAsFactors = F) |>subset(Var2 != Var3)
rbind(a,b) |> unname() |> apply(1,list) |> unlist(F)
- 輸出
[[1]]
[1] "a" "c"
[[2]]
[1] "b" "c"
[[3]]
[1] "a" "d"
[[4]]
[1] "b" "d"
[[5]]
[1] "a" "e"
[[6]]
[1] "b" "e"
$`1`
[1] "a" "c" "d"
$`2`
[1] "b" "c" "d"
$`5`
[1] "a" "c" "e"
$`6`
[1] "b" "c" "e"
$`7`
[1] "a" "d" "e"
$`8`
[1] "b" "d" "e"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505446.html
上一篇:對整數字串進行排序并保存原始索引
下一篇:串列中的多個搜索詞