'How to keep both keys after join with a named "by" in R
With the dplyr join functions, you can use a named by
if the join variables have different names. But the documentation warns "Note that only the key from the LHS is kept". How can I keep both keys?
eg
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)
Joining:
inner_join(df1, df2, by = c("name","dob" = "date_birth"))
# name dob
# Ann 2000-01-01
How do I keep the dob and date_birth columns?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|