rlistcomparisonpairwisecombn

Making a list of pairwise comparisons between specific groups of data


I have two dfs that look something like this:

dat2 <- read.table(text = "
 year  nodepair  `++`  `--`  `+-`  `-+`  `0+`  `+0`  `0-`  `-0`  `00` ES   
1 1999  A1_A1        0     4     0     0     0     0     0     0    16 3    
2 1999  A2_A1        0     0     0     0     0     0     0     0    20 3    
3 1999  A2_A2        0     0     0     0     0     0     0     0    20 4    
4 1999  A3_A1        0     0     0     0     0     2     0     0    18 4    
5 1999  A3_A2        0     0     0     0     0     0     0     0    20 5    
6 1999  A3_A3        0     5     0     0     0     0     0     0    15 5    
", header = TRUE)
dat3 <- read.table(text = "
 year  nodepair  `++`  `--`  `+-`  `-+`  `0+`  `+0`  `0-`  `-0`  `00` ES   
1 1999  A1_A1        0     4     0     0     0     0     0     0    16 8    
2 1999  A2_A1        0     0     0     0     0     0     0     0    20 8    
3 1999  A2_A2        0     0     0     0     0     0     0     0    20 9    
4 1999  A3_A1        0     0     0     0     0     2     0     0    18 9    
5 1999  A3_A2        0     0     0     0     0     0     0     0    20 10    
6 1999  A3_A3        0     5     0     0     0     0     0     0    15 10    
", header = TRUE)

I'm writing a code that uses pairwise lists to compare between the ES groups. Before I was able to successfully put these together and make a pairwise list of ALL combinations using:

dat4 <- rbind(dat2,dat3)
ES_combs <- combn(unique(dat4$ES), 2, simplify = FALSE)

but this time I need to make a pairwise list between these two different groups of ESs to make my new function. so 3-8, 3-9, 3-10, 4-8... etc and not within the set (not 3-4, 4-5).


Solution

  • I think you are looking for

    > x = expand.grid(unique(dat2$ES), unique(dat3$ES))
    > x = x[order(x$Var1), ]
    > paste0(x$Var1, "-", x$Var2)
    [1] "3-8"  "3-9"  "3-10" "4-8"  "4-9"  "4-10" "5-8"  "5-9"  "5-10"
    

    which is an extension of @CPB's comment.