本文介绍了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") 会匹配 xayb

但是,是否可以加入变量组合,或者我是否必须事先添加组合键?

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") 匹配 [xaxc 的串联>] 到 [ybyd]

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()by 参数:

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 可以加入多列或复合键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 01:10