【问题标题】:How to build permutation with some conditions in R [duplicate]如何在R中建立具有某些条件的排列[重复]
【发布时间】:2018-10-06 03:35:16
【问题描述】:

我是 R 新手,我有点困惑。假设我有一个 c(1,2,3,4,5,6) 的向量。我想用四个元素生成排列,每个排列都应该涉及 1 和 5。谢谢。

【问题讨论】:

  • 参考链接的问答。使用MixedCombnPerm(list(c(1,5), c(2,3,4,6)), c(2, 2), TRUE)

标签: r permutation


【解决方案1】:

您可以使用gtools 包中的permutations() 来获取排列,并使用dplyr 中的filter_all() 来获取只有1 和5 的排列:

library(gtools)
library(dplyr)

data <- c(1, 2, 3, 4, 5, 6)

permutations(n = 6, r = 4, v = data, repeats.allowed = FALSE) %>%
  as_tibble() %>% 
  filter_all(any_vars(. == 5)) %>%
  filter_all(any_vars(. == 1))

输出:

# A tibble: 144 x 4
      V1    V2    V3    V4
   <dbl> <dbl> <dbl> <dbl>
 1     1     2     3     5
 2     1     2     4     5
 3     1     2     5     3
 4     1     2     5     4
 5     1     2     5     6
 # ...

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2019-10-22
    • 1970-01-01
    相关资源
    最近更新 更多