简体   繁体   English

打印 R 中字符向量的所有排列

[英]Print all permutations of a vector of characters in R

For starters, forgive my math terminology ignorance (I'll edit this question, once errors in my vocabulary are pointed out).首先,请原谅我对数学术语的无知(一旦指出我的词汇错误,我将编辑这个问题)。

How can I print a complete set of 3 element permutation of a 10 element vector in R?如何在 R 中打印 10 个元素向量的一组完整的 3 个元素排列?

Let's assume a vector consists of 10 unique letters A to J假设一个向量由 10 个不同的字母 A 到 J 组成

x<- LETTERS[seq( from = 1, to = 10 )]

I'd like to list (print) all possible 3 unique element permutations, for example:我想列出(打印)所有可能的3 个唯一元素排列,例如:

ABC, ACB, ABD, ADB ... etc ABC、ACB、ABD、ADB……

Thank you for any hints谢谢你的任何提示

This can be easily done using the gtools package. 这可以使用gtools包轻松完成。

prm <- gtools::permutations(n=10, r=3, v=LETTERS[1:10])

Then you can apply paste0 across the rows to get a vector. 然后,您可以跨行应用paste0以获取向量。

apply(prm, 1, function(x)paste0(x, collapse=''))

类似于collin使用新管道,您可以写为:

LETTERS[1:10] |> permutations(n=10,r=3) |> apply(1,paste, collapse = '')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM