将字符向量中元素的唯一组合连接到 R 中的新字符串向量中
Concatenate unique combinations of elements in a character vector into new vector of strings in R
我正在尝试找到一种简单的方法(最好是单行方法)将字符向量中元素的唯一组合合并并连接到新的字符串向量中。
我还希望能够在插入的矢量组合之前、之间或之后在新矢量中包含任何文本行。组合不应以相反的顺序重复(例如 'x1_x2'
而不是 'x2_x1'
),元素也不应与自身组合(不是 'x1_x1'
)。
是否存在对此的快速解决方案?
所需结果的等效代码示例:
vec <- paste0("X", 1:5)
# The underscore signifies any arbitrary line of text
c(
paste0("_", vec[1], "_", vec[2:5], "_"),
paste0("_", vec[2], "_", vec[3:5], "_"),
paste0("_", vec[3], "_", vec[4:5], "_"),
paste0("_", vec[4], "_", vec[5], "_")
)
'_X1_X2_''_X1_X3_''_X1_X4_''_X1_X5_''_X2_X3_''_X2_X4_''_X2_X5_'
'_X3_X4_''_X3_X5_''_X4_X5_'
你可以使用
apply(combn(vec, 2), 2, \(x) paste(x, collapse = "_"))
#> [1] "X1_X2" "X1_X3" "X1_X4" "X1_X5" "X2_X3" "X2_X4" "X2_X5" "X3_X4" "X3_X5" "X4_X5"
尝试combn
> sprintf("_%s_", combn(vec, 2, paste0, collapse = "_"))
[1] "_X1_X2_" "_X1_X3_" "_X1_X4_" "_X1_X5_" "_X2_X3_" "_X2_X4_" "_X2_X5_"
[8] "_X3_X4_" "_X3_X5_" "_X4_X5_"
> paste0("_", combn(vec, 2, paste0, collapse = "_"), "_")
[1] "_X1_X2_" "_X1_X3_" "_X1_X4_" "_X1_X5_" "_X2_X3_" "_X2_X4_" "_X2_X5_"
[8] "_X3_X4_" "_X3_X5_" "_X4_X5_"
这是另一个使用 arrangements::combinations
的选项:
paste0("_",
apply(arrangements::combinations(x = vec, k = 2), 1, paste, collapse = "_"),
"_")
#[1] "_X1_X2_" "_X1_X3_" "_X1_X4_" "_X1_X5_" "_X2_X3_" "_X2_X4_" "_X2_X5_" "_X3_X4_" "_X3_X5_" "_X4_X5_"
这是使用 crossing
的 tidyverse
版本:
library(tidyverse)
crossing(x=vec, y=vec) %>%
mutate(new = paste0("_",x,"_",y,"_")) %>%
group_by(x) %>%
filter(row_number()!= 1:unique(parse_number(x))) %>%
pull(new)
[1] "_X1_X2_" "_X1_X3_" "_X1_X4_" "_X1_X5_" "_X2_X3_"
[6] "_X2_X4_" "_X2_X5_" "_X3_X4_" "_X3_X5_" "_X4_X5_"
我正在尝试找到一种简单的方法(最好是单行方法)将字符向量中元素的唯一组合合并并连接到新的字符串向量中。
我还希望能够在插入的矢量组合之前、之间或之后在新矢量中包含任何文本行。组合不应以相反的顺序重复(例如 'x1_x2'
而不是 'x2_x1'
),元素也不应与自身组合(不是 'x1_x1'
)。
是否存在对此的快速解决方案?
所需结果的等效代码示例:
vec <- paste0("X", 1:5)
# The underscore signifies any arbitrary line of text
c(
paste0("_", vec[1], "_", vec[2:5], "_"),
paste0("_", vec[2], "_", vec[3:5], "_"),
paste0("_", vec[3], "_", vec[4:5], "_"),
paste0("_", vec[4], "_", vec[5], "_")
)
'_X1_X2_''_X1_X3_''_X1_X4_''_X1_X5_''_X2_X3_''_X2_X4_''_X2_X5_'
'_X3_X4_''_X3_X5_''_X4_X5_'
你可以使用
apply(combn(vec, 2), 2, \(x) paste(x, collapse = "_"))
#> [1] "X1_X2" "X1_X3" "X1_X4" "X1_X5" "X2_X3" "X2_X4" "X2_X5" "X3_X4" "X3_X5" "X4_X5"
尝试combn
> sprintf("_%s_", combn(vec, 2, paste0, collapse = "_"))
[1] "_X1_X2_" "_X1_X3_" "_X1_X4_" "_X1_X5_" "_X2_X3_" "_X2_X4_" "_X2_X5_"
[8] "_X3_X4_" "_X3_X5_" "_X4_X5_"
> paste0("_", combn(vec, 2, paste0, collapse = "_"), "_")
[1] "_X1_X2_" "_X1_X3_" "_X1_X4_" "_X1_X5_" "_X2_X3_" "_X2_X4_" "_X2_X5_"
[8] "_X3_X4_" "_X3_X5_" "_X4_X5_"
这是另一个使用 arrangements::combinations
的选项:
paste0("_",
apply(arrangements::combinations(x = vec, k = 2), 1, paste, collapse = "_"),
"_")
#[1] "_X1_X2_" "_X1_X3_" "_X1_X4_" "_X1_X5_" "_X2_X3_" "_X2_X4_" "_X2_X5_" "_X3_X4_" "_X3_X5_" "_X4_X5_"
这是使用 crossing
的 tidyverse
版本:
library(tidyverse)
crossing(x=vec, y=vec) %>%
mutate(new = paste0("_",x,"_",y,"_")) %>%
group_by(x) %>%
filter(row_number()!= 1:unique(parse_number(x))) %>%
pull(new)
[1] "_X1_X2_" "_X1_X3_" "_X1_X4_" "_X1_X5_" "_X2_X3_"
[6] "_X2_X4_" "_X2_X5_" "_X3_X4_" "_X3_X5_" "_X4_X5_"