简体   繁体   English

在R中查找对象的组合

[英]Find combinations of objects in R

I have the following example code: 我有以下示例代码:

library(caTools)
sample1 = rnorm(20)
sample2 = rnorm(30)
sample3 = rnorm(40)
# could be more samples

args = list(sample1, sample2, sample3) # could be more

> combs(c(args), k=2)
     [,1]       [,2]      
[1,] Numeric,20 Numeric,30
[2,] Numeric,20 Numeric,40
[3,] Numeric,30 Numeric,40

However, this is not what is desired. 然而,这不是所期望的。 I would like to feed combs input that should give the same as: 我想提供combs输入,应该给出相同的:

> combs(c("sample1","sample2", "sample3"),k=2)
     [,1]      [,2]     
[1,] "sample1" "sample2"
[2,] "sample1" "sample3"
[3,] "sample2" "sample3"

and from there I would want to use get to extract the vectors for each sampleX object by row. 从那里我想用getsampleX提取每个sampleX对象的向量。

How can I do this without hardcoding "sample1", "sample2", etc. so that I can have as many as samples as are fed to it? 如何在不对“sample1”,“sample2”等进行硬编码的情况下执行此操作,以便我可以提供与其一样多的样本?

From library(gtools) : 来自library(gtools)

combinations(3,2,c("sample1","sample2", "sample3"))

Result: 结果:

     [,1]      [,2]     
[1,] "sample1" "sample2"
[2,] "sample1" "sample3"
[3,] "sample2" "sample3"

The same result can be obtained if those objects are named elements of a list: 如果这些对象是列表的命名元素,则可以获得相同的结果:

tmp <- list(sample1=1:3,sample2=4:6,sample3=7:9)
combinations(3,2,names(tmp))

Or, if those objects are all in an environment: 或者,如果这些对象都在一个环境中:

tmp <- new.env()
tmp$sample1 <- 1:3
tmp$sample2 <- 4:6
tmp$sample3 <- 7:9
combinations(3,2,objects(tmp))

How about this? 这个怎么样? I use simplified data as an illustrative example. 我使用简化数据作为说明性示例。

Edit 编辑

Thanks to @GSee for recommending two improvements in this approach [see comment]. 感谢@GSee推荐这种方法的两项改进[见评论]。

This is not something I'd be keen to do, but we use ls and the pattern argument on the names of all objects in your global environment to return the names of those that fit the pattern ie all objects which include "sample" in the object names - so be careful - and then stick them in a list using mget . 这不是我想要做的事情,但是我们在全局环境中的所有对象的名称上使用lspattern参数来返回符合模式的名称,即包含"sample"所有对象。对象名称 - 所以要小心 - 然后使用mget它们粘贴到list中。

We then get the combinations of list elements using combn and use an anonymous function to combine all elements of list pairs using expand.grid . 然后,我们使用获取列表元素的组合combn并使用匿名函数使用列表对所有元素结合expand.grid If you want this as a two column data.frame you can use do.call and rbind the returned list together: 如果您希望将其作为两列data.frame ,则可以使用do.call并将返回的列表一起rbind

sample1 <- 1:2
sample2 <- 3:4
sample3 <- 5:6

args <-mget( ls( pattern = "^sample\\d+") , env = .GlobalEnv )

res <- combn( length(args) , 2 , FUN = function(x) expand.grid(args[[x[1]]] , args[[x[2]]]) , simplify = FALSE )

do.call( rbind , res )
   Var1 Var2
1     1    3
2     2    3
3     1    4
4     2    4
5     1    5
6     2    5
7     1    6
8     2    6
9     3    5
10    4    5
11    3    6
12    4    6

Here is an approach 这是一种方法

# put samples in separate structure, for instance a list
samples <- list(s1=rnorm(20), s2=rnorm(30), s3=rnorm(40))     

cmb <- t(combn(names(samples),m=2))
apply(cmb,1,FUN=function(x) list(samples[[x[[1]]]], samples[[x[[2]]]]))

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

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