【问题标题】:R: generate possible permutation tables by one columnR:按一列生成可能的排列表
【发布时间】:2015-03-07 04:33:19
【问题描述】:

我有一个如下所示的表格:

 Indikaatori nimi Alamkriteerium Kriteerium Skoor
1  Indikaator    1            1.1          1   100
2  Indikaator    2            1.2          1   100
3  Indikaator    3            1.3          1   100
4  Indikaator    4            1.1          1     0
5  Indikaator    5            2.1          2     0
6  Indikaator    6            2.1          2     0
... and so on...

我需要按第一列创建表格的所有可能排列。 共有 50 个指标,我想从中挑选 49 个并获得所有可能的组合以及所选元素的其他数据列。 使用 50 个元素中的 49 个元素,我将获得总共 50 个排列,但我想自动创建所有这些表,而无需手动操作(稍后还需要 48 个元素)。

有什么方法可以自动生成这 50 个表格,其中包含所选元素的相应数据?

感谢所有帮助和指点!

【问题讨论】:

  • 不要将排列与组合混淆。组合是不同的子集。排列是不同的顺序。你想要的是组合。

标签: r combinations permutation


【解决方案1】:
# The following will give you a list of fifty data frames,
# Each data frame has a 49 row subset of the original
listoftables <- apply(combn(1:50, 49), 2, FUN = function(x) df[x,])

【讨论】:

  • 有没有办法可以追加到某个表?这确实为我提供了必要的表格,但现在我还想将它们分成单独的表格,我想单独处理。感谢您的帮助!
  • 这样的? thisdf
  • 非常感谢!这正是我所需要的!
  • 请注意,您可以将上面的“49”更改为“48”并取回您说稍后需要的 1225 个子集数据帧(每个 48 行)。
【解决方案2】:

此解决方案使用循环,与 R 中的矢量化操作相比,它相当慢,但它会以 data.frames 列表的形式为您提供所需的内容。

datatable = read.table(textConnection(
  "2  Indikaator    2            1.2          1   100
3  Indikaator    3            1.3          1   100
4  Indikaator    4            1.1          1     0
5  Indikaator    5            2.1          2     0
6  Indikaator    6            2.1          2     0"))


x = rep(list(data.frame(NULL)),times = 2^nrow(datatable))
a = 1
for (i in 1:nrow(datatable)){
  sets = combn(nrow(datatable),i)
  for (j in 1:ncol(sets)){
    x[[a]] = datatable[sets[,j],]
    a = a+1
  }
}
View(x[[10]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-05
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    相关资源
    最近更新 更多