友 我有一个简单的问题,但无法巧妙地修复它。以下是它的外观......
df1 --> this data frame has around 3mn rows
event lat long
e01010 10.1010 20.1010
e02020 10.1010 20.1010
e03030 10.1010 20.1010
e04040 10.1010 20.1010
.
.
.
df2 --> this data frame has around 60k rows
event start_date end_date
e01010 2016-01-10 2016-01-12
e04020 2017-10-12 2017-10-22
e03030 2015-01-10 2015-01-10
e06040 2018-01-22 2018-02-22
.
.
.
现在我期待“df2”中的结果如下所示,新增了2列名为“lat”和“long”
df2
event start date end date lat long
e01010 2016-01-10 2016-01-12 10.1010 20.1010
e04020 2017-10-12 2017-10-22 NA NA
e03030 2015-01-10 2015-01-10 10.1010 20.1010
e06040 2018-01-22 2018-02-19 NA NA
.
.
.
正如你所看到的,df2是我的主要数据框,我想用df1附加匹配条件的列。
任何人都可以在这里帮助我..我试过“哪个”但是无法实现!!
答案 0 :(得分:1)
您可以使用dplyr::left_join
:
dplyr::left_join(df2, df1, by = "event");
# event start.date end.date lat long
#1 e01010 2016-01-10 2016-01-12 10.101 20.101
#2 e04020 2017-10-12 2017-10-22 NA NA
#3 e03030 2015-01-10 2015-01-10 10.101 20.101
#4 e06040 2018-01-22 2018-02-22 NA NA
或在基地R:
merge(df2, df1, by = "event", all.x = TRUE);