2019-08-04

How to do cross join in R?

Problem

We want to calculate a cross join, the Cartesian product of rows from tables —or elements of a vector— in the join. In other words, we want to combine each row from the first table with each row from the second table.

Solution

Two vectors

  • expand.grid
  • expand.grid(1:5, 1:5)
    
  • merge
  • merge(1:5, 1:5)
    
  • Cross join with sqldf
  • library(sqldf)
    df1 <- data.frame(a = 1:5)
    df2 <- df1
    sqldf("SELECT df1.a, df2.a FROM df1 
          CROSS JOIN df2")
    
  • Results
  •    x y
    1  1 1
    2  2 1
    3  3 1
    4  4 1
    5  5 1
    6  1 2
    7  2 2
    8  3 2
    9  4 2
    10 5 2
    11 1 3
    12 2 3
    13 3 3
    14 4 3
    15 5 3
    16 1 4
    17 2 4
    18 3 4
    19 4 4
    20 5 4
    21 1 5
    22 2 5
    23 3 5
    24 4 5
    25 5 5
    
More than two vectors

  • expand.grid.
  • a <- c('europe', 'asia')
    b <- c('co2', 'temperature')
    c <- c('min', 'max')
    expand.grid(a, b, c)
    
        Var1        Var2 Var3
    1 europe         co2  min
    2   asia         co2  min
    3 europe temperature  min
    4   asia temperature  min
    5 europe         co2  max
    6   asia         co2  max
    7 europe temperature  max
    8   asia temperature  max
    

References

No hay comentarios:

Publicar un comentario

Nube de datos