Running dplyr full_join without a shared variable to mix data frames

Using the dplyr operation full_join(), I am trying to perform the equivalent of a basic operation merge()in which there are no common variables (it is impossible to satisfy the by by = argument). This will mix two data frames and return all possible combinations.

However, the current function full_join()requires a shared variable. I cannot find another dplyr function that can help with this. How to perform this operation using functions specific to the dplyr library?

df_a = data.frame(department=c(1,2,3,4))
df_b = data.frame(period=c(2014,2015,2016,2017))

#This works as desired
big_df = merge(df_a,df_b)

#I'd like to perform the following in a much bigger operation:
big_df = dplyr::full_join(df_a,df_b)

#Error: No common variables. Please specify `by` param.
+4
source share
1 answer

You can use crossingfrom tidyr:

crossing(df_a,df_b)

   department period
1           1   2014
2           1   2015
3           1   2016
4           1   2017
5           2   2014
6           2   2015
7           2   2016
8           2   2017
9           3   2014
10          3   2015
11          3   2016
12          3   2017
13          4   2014
14          4   2015
15          4   2016
16          4   2017
+15
source

Source: https://habr.com/ru/post/1680447/


All Articles