rcombn

Produce all combinations of one element with all other elements within a single column in R


Suppose I have a data frame with a single column that contains letters a, b, c, d, e.

a
b
c
d
e

In R, is it possible to extract a single letter, such as 'a', and produce all possible paired combinations between 'a' and the other letters (with no duplications)? Could the combn command be used in this case?

a b
a c
a d
a e

Solution

  • We can use data.frame

    data.frame(col1 = 'a', col2 = setdiff(df1$V1, "a"))
    

    -ouptput

    col1 col2
    1    a    b
    2    a    c
    3    a    d
    4    a    e
    

    data

    df1 <- structure(list(V1 = c("a", "b", "c", "d", "e")),
        class = "data.frame", row.names = c(NA, 
    -5L))