1

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(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.

How can I do this without hardcoding "sample1", "sample2", etc. so that I can have as many as samples as are fed to it?

GSee
  • 48,880
  • 13
  • 125
  • 145
Kaleb
  • 1,022
  • 1
  • 15
  • 26
  • `combs` being a function from where? – Thomas Jul 22 '13 at 20:35
  • 'caTools' package. I just find it installed on my system. – Kaleb Jul 22 '13 at 20:36
  • 1
    Look at http://stackoverflow.com/questions/10520772/in-r-how-to-get-an-objects-name-from-the-object-itself But nevertheless you will need to explicitly list all objects. Maybe the better way will be to combine all samples in a list, create combinations of indices of samples in the list and use them. – DrDom Jul 22 '13 at 20:52
  • Just as implemented with the indices. Excellent suggestion. I'd mark this as the answer. – Kaleb Jul 22 '13 at 21:01
  • @DrDom You should post that as an answer. – Thomas Jul 22 '13 at 21:04

3 Answers3

2

From 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))
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • What I meant was, how can I not put in `c("sample1","sample2", "sample3")` when all of the elements of these vectors refer to an actual object. I want to capture the names of these objects and get the different combinations of them. – Kaleb Jul 22 '13 at 20:48
  • @Kaleb Where are the objects? Can you put them in a list? Or in their own environment? – Thomas Jul 22 '13 at 20:49
  • @Kaleb Ah, I see. I think my edits suggesting using an environment - which is basically what DrDom just commented to you - is probably the way to go. Then you can use `get("sample1",envir=tmp)`, etc. to retrieve the objects from that environment. – Thomas Jul 22 '13 at 21:01
1

How about this? I use simplified data as an illustrative example.

Edit

Thanks to @GSee for recommending two improvements in this approach [see comment].

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 i.e. all objects which include "sample" in the object names - so be careful - and then stick them in a list using mget.

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. If you want this as a two column data.frame you can use do.call and rbind the returned list together:

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
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
0

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]]]]))
Karsten W.
  • 17,826
  • 11
  • 69
  • 103
  • See comment to Thomas. This also does not address how to capture object references as strings, AND then find the different combinations of these references. – Kaleb Jul 22 '13 at 20:49