【问题标题】:combination without repetition in R between vector elements向量元素之间的 R 中不重复的组合
【发布时间】:2022-02-25 18:28:50
【问题描述】:

我有一个向量x <- c(1,2,3,4,5),我想写出一个元素和所有其他元素之间的所有组合,例如。 12, 13, 14, 15 等..,也没有 21。

comb <- expand.grid(rep(list(x), 2))

我正在使用这个命令(在这里创建:Combination without repetition in R(第一个答案给出了相同的答案))但我在获得的 df 中有重复。我怎么能避免这种情况?谢谢!

【问题讨论】:

  • combn(x, 2)...?
  • @Sotos combn(x,2) 给了我一个矩阵,我需要一个只有 2 个变量的 df,就像 comb &lt;- expand.grid(rep(list(x), 2)) 的输出一样
  • as.data.frame(t(combn(x, 2)))

标签: r vector combinations


【解决方案1】:

已解决as.data.frame(t(combn(x, 2))) 谢谢!

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 2016-09-19
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    相关资源
    最近更新 更多