cartesian product with dplyr R

前端 未结 6 572
清酒与你
清酒与你 2020-12-03 07:06

I\'m trying to find the dplyr function for cartesian product. I\'ve two simple data.frame with no common variable:

x <- data.frame(x=c(\"a\",\"b\",\"c\"))         


        
6条回答
  •  清歌不尽
    2020-12-03 07:36

    If we need a tidyverse output, we can use expand from tidyr

    library(tidyverse)
    y %>% 
       expand(y, x= x$x) %>%
       select(x,y)
    # A tibble: 9 × 2
    #       x     y
    #   
    #1      a     1
    #2      b     1
    #3      c     1
    #4      a     2
    #5      b     2
    #6      c     2
    #7      a     3
    #8      b     3
    #9      c     3
    

提交回复
热议问题