【问题标题】:Using "combn" to create list of dataframes for all combinations of a variable使用“combn”为变量的所有组合创建数据框列表
【发布时间】:2013-11-28 01:41:16
【问题描述】:

使用下面的数据框并在 R 中工作,我想生成一个新数据框列表,其中每个数据框都基于我的分类向量的 4 个不同值的唯一组合。在下面的数据框中,我有 10 个不同的分类单元(许多有多个重复条目),我想一次选择 4 个分类单元,用于 4 的所有组合。我相信这 10 个分类单元中应该有 4 个分类单元的 210 种不同组合(当没有更换样品时,订单并不重要)。所以最终我想要一个包含 210 个数据帧的列表,每个数据帧包含 4 个分类单元的不同组合(每个分类单元的所有复制行)!!!

虽然选择基于分类向量,但我希望新数据框包含其他信息列。例如,如果选择“Aphididae.sp.3”,我希望新的数据框也有 -25.92(C)、1.69(N)、sap(func.group)、herbivore(trophic.group) 列在列中旁边。

到目前为止,我使用“combn”函数使用“unique”命令生成 4 个分类单元的所有组合,但我无法获取其中包含的所有其他信息列,并且它没有给出所有复制每个分类单元的条目!如有任何帮助,我将不胜感激!

我的代码:

combos<-combn(unique(df$Taxon),4)

df:

                  Taxon      C    N     func.group trophic.grp
1           Aphididae.sp.3 -25.92 1.69        sap   herbivore
2           Aphididae.sp.3 -25.91 1.78        sap   herbivore
3           Aphididae.sp.3 -26.05 1.74        sap   herbivore
4  Cicadellidae.mixed.juvs -28.94 1.19        sap   herbivore
5  Cicadellidae.mixed.juvs -29.25 2.24        sap   herbivore
6  Cicadellidae.mixed.juvs -28.17 1.88        sap   herbivore
7  Cicadellidae.mixed.juvs -28.29 1.94        sap   herbivore
8        Cicadellidae.sp.1 -27.69 2.25        sap   herbivore
9        Cicadellidae.sp.1 -27.67 2.41        sap   herbivore
10       Cicadellidae.sp.1 -26.65 3.26        sap   herbivore
11       Cicadellidae.sp.1 -28.30 3.20        sap   herbivore
12       Cicadellidae.sp.1 -28.08 1.88        sap   herbivore
13       Cicadellidae.sp.2 -26.59 2.89        sap   herbivore
14       Cicadellidae.sp.3 -26.82 5.16        sap   herbivore
15       Cicadellidae.sp.4 -26.54 3.46        sap   herbivore
16       Cicadellidae.sp.4 -26.55 4.05        sap   herbivore
17       Cicadellidae.sp.4 -27.20 3.14        sap   herbivore
18       Cicadellidae.sp.4 -26.48 3.80        sap   herbivore
19       Cicadellidae.sp.5 -27.54 4.17        sap   herbivore
20       Cicadellidae.sp.5 -27.18 3.43        sap   herbivore
21       Cicadellidae.sp.5 -27.46 4.03        sap   herbivore
22       Cicadellidae.sp.6 -26.71 1.09        sap   herbivore
23       Cicadellidae.sp.6 -26.33 1.56        sap   herbivore
24       Cicadellidae.sp.6 -25.59 0.59        sap   herbivore
25       Cicadellidae.sp.6 -25.07 0.84        sap   herbivore
26       Cicadellidae.sp.6 -26.56 0.97        sap   herbivore
27       Cicadellidae.sp.7 -25.84 1.08        sap   herbivore
28       Cicadellidae.sp.7 -24.96 1.36        sap   herbivore
29       Cicadellidae.sp.7 -26.15 1.90        sap   herbivore
30       Cicadellidae.sp.7 -26.58 2.63        sap   herbivore
31       Cicadellidae.sp.8 -28.02 2.28        sap   herbivore
32       Cicadellidae.sp.8 -27.90 2.01        sap   herbivore
33       Cicadellidae.sp.8 -27.70 1.92        sap   herbivore
34       Cicadellidae.sp.8 -26.85 1.04        sap   herbivore

【问题讨论】:

    标签: r combinations


    【解决方案1】:

    combos 应该是一个 4 x 210 的字符向量矩阵。如果您不是使用原始代码,而是使用:

     combos<-combn(unique(as.character(df$Taxon)), 4)   # factors would be converted
    

    您应该能够使用apply 将这些向量传递给子集函数:

    combdf <- apply(combos, 2, function(vec) df[ df$Taxon %in% vec, ] )
    

    在测试过程中,我发现原始矩阵搞砸了,因为我将分类单元作为因子保留,因此需要在 unique 之前调用 as.character。直到尝试apply 调用我才看到这个,因为我有 210 件物品,但其中大部分是空的。

    【讨论】:

    • 非常感谢您快速而出色的回复。非常感谢,马特
    【解决方案2】:
    lapply(ncol(combos), function(x) df[df$Taxon %in% combos[,x],])
    

    【讨论】:

    • 感谢您的替代方法!
    • 您能再解释一下吗?我知道它对列表、组合文件中的列进行了应用,并且它执行了一个函数……但我没有得到构成该函数的 %in% 内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    相关资源
    最近更新 更多