0

Consider this data frame:

set.seed(123)
dat1 <- data.frame(Loc = rep(letters[1:20], each = 10),
                   ID = 1:200,
                   var1 = rnorm(200))
dat1$ID <- factor(dat1$ID)

I want to return a data frame that shows all possible pairwise comparisons that could be performed on the levels of Loc, which would look like this:

Loc1  Loc2
a       b
a       c
a       d
a       e
…
#and so on, just like a post hoc test would return (e.g. Tukey's)

But how do you do this between the levels of a single column?

Ryan
  • 1,048
  • 7
  • 14

2 Answers2

0

You can use

new_df <- expand.grid(Loc1 = levels(dat1$Loc), Loc2 = levels(dat1$Loc))

And if we examine the first 25 rows we see

head(new_df, 25)
#>    Loc1 Loc2
#> 1     a    a
#> 2     b    a
#> 3     c    a
#> 4     d    a
#> 5     e    a
#> 6     f    a
#> 7     g    a
#> 8     h    a
#> 9     i    a
#> 10    j    a
#> 11    k    a
#> 12    l    a
#> 13    m    a
#> 14    n    a
#> 15    o    a
#> 16    p    a
#> 17    q    a
#> 18    r    a
#> 19    s    a
#> 20    t    a
#> 21    a    b
#> 22    b    b
#> 23    c    b
#> 24    d    b
#> 25    e    b
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
0

Maybe combn could help

setNames(as.data.frame(t(combn(unique(dat1$Loc),2))),c("Loc1","Loc2"))

such that

> head(setNames(as.data.frame(t(combn(unique(dat1$Loc),2))),c("Loc1","Loc2")))
  Loc1 Loc2
1    a    b
2    a    c
3    a    d
4    a    e
5    a    f
6    a    g
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81