本文介绍了在没有公共变量的情况下执行dplyr full_join来混合数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用dplyr full_join()操作,我试图执行与基本 merge()等效的操作不存在公共变量的操作(无法满足 by =参数)。这将混合两个数据帧并返回所有可能的组合。

Using the dplyr full_join() operation, I am trying to perform the equivalent of a basic merge() operation in which no common variables exist (unable to satisfy the "by=" argument). This will blend two data frames and return all possible combinations.

但是,当前的 full_join()函数需要一个公共变量。我找不到其他可以帮助您解决此问题的dplyr函数。 如何使用特定于dplyr库的功能执行此操作?

However, the current full_join() function requires a common variable. I am unable to locate another dplyr function that can help with this. How can I 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.


推荐答案

您可以使用 crossing 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

这篇关于在没有公共变量的情况下执行dplyr full_join来混合数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:09