r - 具有重复元素的样本的 2 到 2 的排列
问题描述
如何获得具有重复元素的列表的所有可能排列?例如, 2 by 与x = x (1,2,2)
我想要重复排列的向量:
1 1
1 2
1 2
2 1
2 2
2 2
2 1
2 2
2 2
解决方案
使用用于生成重复排列的众多软件包之一很容易实现这一点。
library(gtools)
gtools::permutations(3, 2, c(1, 2, 2), set = FALSE, repeats.allowed = TRUE)
[,1] [,2]
[1,] 1 1
[2,] 1 2
[3,] 1 2
[4,] 2 1
[5,] 2 2
[6,] 2 2
[7,] 2 1
[8,] 2 2
[9,] 2 2
library(arrangements)
arrangements::permutations(x = c(1,2,2), k = 2, replace = TRUE)
## output same as above
library(RcppAlgos) ### I am the author
RcppAlgos::permuteGeneral(c(1,2,2), 2, TRUE)
## output same as above
推荐阅读
- reactjs - 如何使用 setState(带有类组件)从对象数组中更新单个属性?
- java - Android Camera2 - 如何为不同的手机型号和版本设置相机焦距
- android - 为什么要在我的服务 url 中添加“?roundtrip=true”?
- ruby-on-rails - 重命名资源的索引页面上的 Rails ActiveAdmin 路由问题
- c# - 如何为不同的客户创建不同的 DLL
- informatica - 无法在 Informatica 映射中捕获参数值
- kubernetes - kubernetes 服务是否有一种简单的方法可以为服务器端实现 oauth 2.0?
- php - 如何根据标记数字或逗号启动 PHP 循环
- elasticsearch - Elasticsearch 检查单词子集是否存在
- ios - 以 NSException Timer Swift Crash 类型的未捕获异常终止