首页 > 解决方案 > 如何在R中的向量中找到唯一的数字对?

问题描述

让我们假设C<-c(1,2,3,4,5) 我想找到可以从该向量中提取的所有唯一数字对,例如 12,13 23 等。我该怎么做?

标签: r

解决方案


使用RcppAlgos包。

## Combinations
unlist(RcppAlgos::comboGeneral(x, 2, FUN=function(x) Reduce(paste0, x)))
# [1] "12" "13" "14" "15" "23" "24" "25" "34" "35" "45"

## Permutations
unlist(RcppAlgos::permuteGeneral(x, 2, FUN=function(x) Reduce(paste0, x)))
# [1] "12" "13" "14" "15" "21" "23" "24" "25" "31" "32" "34" "35" "41" "42" "43"
# [16] "45" "51" "52" "53" "54"

推荐阅读