-1

Friends, I have a simple problem, but could not fix it smartly. Below is how it looks..

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
.
.
.

Now i'm expecting result as below in "df2" with new additional 2 columns called "lat" and "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
.
.
.

As you can see df2 is my main data frame and i wanted to append columns with matching conditions with df1..

can any one please help me here.. i tried "which" but could not achieve!!

Adarsha Murthy
  • 145
  • 3
  • 13

1 Answers1

1

You can use 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

Or in base R:

merge(df2, df1, by = "event", all.x = TRUE);
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68