将两个数据框与所有可能的组合结合起来 [英] combine two data frames with all posible combinations

查看:21
本文介绍了将两个数据框与所有可能的组合结合起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个数据框.
我如何使用 tidyverse 与他们一起制作类似 tidyr::complete 的东西?

I have 2 data frames.
How I can make something like tidyr::complete with them using tidyverse?

我的数据:

df <-data.frame(a=letters[1:2] )
df1<-data.frame(one=1:2)

预期结果:

a 1 
b 1
a 2
b 2

谢谢!

推荐答案

通过这个特定的例子,我认为你可以使用合并功能.作为标准,它的参数 all.x 和 all.y 被设置为 TRUE,因此它会自动创建所有组合,因为数据帧没有任何共同的变量或值.

With this particular example I think you can just use the merge function. As a standard its arguments all.x and all.y are set to TRUE, so it automatically creates all combinations since the dataframes do not have any variables or values in common.

df <-data.frame(a=letters[1:10] )
df1<-data.frame(one=1:10)

dfcomb <- merge(df,df1)
dim(dfcomb) 
[1] 100   2 #gives 100 rows and 2 columns

这篇关于将两个数据框与所有可能的组合结合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆