dplyr可以在多个列或组合键上联接吗? [英] Can dplyr join on multiple columns or composite key?

查看:63
本文介绍了dplyr可以在多个列或组合键上联接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到 dplyr v3.0允许您加入不同的变量:

I realize that dplyr v3.0 allows you to join on different variables:

left_join(x,y,by = c( a = b) xa 匹配为 yb

但是,是否可以结合使用变量的组合,还是必须事先添加复合键?

However, is it possible to join on a combination of variables or do I have to add a composite key beforehand?

类似这样的东西:

left_join(x,y,by = c( ac = bd)匹配[ xa xc ]的串联到[ yb yd ]

left_join(x, y, by = c("a c" = "b d") to match the concatenation of [x.a and x.c] to [y.b and y.d]

推荐答案

更新为使用tibble()

您可以将长度大于1的命名向量传递给通过 left_join()参数:

You can pass a named vector of length greater than 1 to the by argument of left_join():

library(dplyr)

d1 <- tibble(
  x = letters[1:3],
  y = LETTERS[1:3],
  a = rnorm(3)
  )

d2 <- tibble(
  x2 = letters[3:1],
  y2 = LETTERS[3:1],
  b = rnorm(3)
  )

left_join(d1, d2, by = c("x" = "x2", "y" = "y2"))

这篇关于dplyr可以在多个列或组合键上联接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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