cartesian product with dplyr R

前端 未结 6 558
清酒与你
清酒与你 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:39

    expand.grid(x=c("a","b","c"),y=c(1,2,3))
    

    Edit: Consider also this following elegant solution from "Y T" for n more complex data.frame :

    https://stackoverflow.com/a/21911221/5350791

    in short:

    expand.grid.df <- function(...) Reduce(function(...) merge(..., by=NULL), list(...))
    expand.grid.df(df1, df2, df3)
    

提交回复
热议问题