1

How do you find all possible unique pairwise comparisons of variables with various levels (with 3 example scenarios) using R for the mtcars data? Are there ways to do this using r code in different scenarios?

Here are the examples I’m interested in:

Example 1: I have 1 factor variable (i.e., “cyl”) with 3 levels (i.e., "4" , “6”, and “8”) that is in 1 column of a data frame. Subjects can be only one level of the one variable, and all subjects have some value associated with the variable in a given row of 1 column. Is there a way to use R code to find all of the unique 2-degree pairwise comparisons of this 1 variable with 3 levels (e.g., mtcars$cyl == 4 and mtcars$cyl == 5, mtcars$cyl == 4 and mtcars$cyl == 6, etc...)?

Example 2: I have 1 factor variable (i.e., "cyl") with 3 levels (i.e., "4" , “6”, and “8”) and another factor variable (i.e., "Engine") with 2 levels (i.e., “0 = V-shaped”, “1 = straight”). Subjects can be only one level of each variable, and all subjects have values associated with the 2 variables in given rows of 2 columns. Is there a way to figure out all of the unique 2-degree pairwise comparisons for the 2 variables (e.g., mtcars$cyl == 4 and mtcars$Engine == 0, mtcars$cyl == 6 and mtcars$Engine == 1, etc…) using R code?

Example 3: I have 1 factor variable (i.e., "cyl") with 3 levels (i.e., "4" , “6”, and “8”), another factor variable (i.e., "Engine") with 2 levels (i.e., “0 = V-shaped”, “1 = straight”), and a 3rd factor variable (i.e., am) with 2 levels (i.e., “0 = automatic”, “1 = manual”). Subjects can be only one level of each variable, and all subjects have values associated with the 3 variables in given rows of 3 columns. Is there a way to figure out all of the unique 3-degree pairwise comparisons for the 3 variables (e.g., mtcars$cyl == 4 and mtcars$Engine == 0 and mtcars$am = 0, mtcars$cyl == 6 and mtcars$Engine == 1 and mtcars$am = 1, etc…) using R code?

Thanks in advance.

Mel
  • 510
  • 3
  • 10

2 Answers2

3

You can also use crossing function from tidyr package:

    library(tidyr)
    crossing(mtcars$cyl, mtcars$vs, mtcars$am)
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
2

Not sure what you are finally trying to achieve but for your first example you could make use of combn and for examples 2 and 3 you may use expand.grid to get a data frame of unique combinations of levels of one or more factors:

# Example 1
as.data.frame(t(combn(unique(mtcars$cyl), 2)))
#>   V1 V2
#> 1  6  4
#> 2  6  8
#> 3  4  8
# Example 2
expand.grid(cyl = unique(mtcars$cyl), vs = unique(mtcars$vs))
#>   cyl vs
#> 1   6  0
#> 2   4  0
#> 3   8  0
#> 4   6  1
#> 5   4  1
#> 6   8  1
# Example 3
expand.grid(cyl = unique(mtcars$cyl), vs = unique(mtcars$vs), am = unique(mtcars$am))
#>    cyl vs am
#> 1    6  0  1
#> 2    4  0  1
#> 3    8  0  1
#> 4    6  1  1
#> 5    4  1  1
#> 6    8  1  1
#> 7    6  0  0
#> 8    4  0  0
#> 9    8  0  0
#> 10   6  1  0
#> 11   4  1  0
#> 12   8  1  0
stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thanks. This was exactly what I needed. I tried to ask this in a previous thread, but it was poorly written and didn't use a dataset that could be easily identified, so I changed stuff to mtcars. – Mel Jan 09 '21 at 22:40