使用dplyr连接函数,如果连接变量具有不同的名称,则可以使用命名的by
。但是文档警告“请注意,仅保留LHS的密钥”。如何保留两个键?
例如
library(dplyr)
df1 <- data.frame(name= c("Ann", "Betsy", "Charlie", "Dave"),
dob= c(as.Date("2000-01-01", "%Y-%m-%d"),
as.Date("2001-01-01", "%Y-%m-%d"),
as.Date("2002-01-01", "%Y-%m-%d"), as.Date("2003-01-01", "%Y-%m-%d")),
stringsAsFactors=FALSE)
df2 <- data.frame(name= c("Ann", "Charlie", "Elmer", "Fred"),
date_birth= c(as.Date("2000-01-01", "%Y-%m-%d"),
as.Date("2004-01-01", "%Y-%m-%d"),
as.Date("2001-01-01", "%Y-%m-%d"),
as.Date("2006-01-01", "%Y-%m-%d")), stringsAsFactors=FALSE)
加入:
inner_join(df1, df2, by = c("name","dob" = "date_birth"))
# name dob
# Ann 2000-01-01
如何保留dob和date_birth列?