I have two dataframe(df1, df2) with different size . The df2 is subset of df1.
Date columns are formatted as date (y.m.d). (Location and location_id have a one to one mapping.)
tl;dr my goal is to use df2’s date to filter any date that comes after that for its specific location. df1
| date | location | location_id | y | x |
|---|---|---|---|---|
| 22.02.02 | A | 1 | 26 | 78 |
| 22.02.02 | B | 2 | 45 | 67 |
| 22.02.02 | C | 3 | 46 | 57 |
| 22.02.02 | D | 4 | 27 | 89 |
| 22.02.01 | A | 1 | 37 | 67 |
| 22.02.01 | B | 2 | 82 | 23 |
| 22.02.01 | C | 3 | 56 | 68 |
| 22.02.01 | D | 4 | 67 | 90 |
| 22.01.31 | A | 1 | 61 | 37 |
| 22.01.31 | B | 2 | 90 | 65 |
| 22.01.31 | C | 3 | 43 | 34 |
| 22.01.31 | D | 4 | 12 | 23 |
| 22.01.30 | A | 1 | 38 | 48 |
| 22.01.30 | B | 2 | 57 | 53 |
| 22.01.30 | C | 3 | 75 | 95 |
| 22.01.30 | D | 4 | 76 | 19 |
df2
| date | location | location_id | y | x |
|---|---|---|---|---|
| 22.02.01 | A | 1 | 37 | 67 |
| 22.02.02 | B | 2 | 45 | 67 |
| 22.01.30 | C | 3 | 75 | 95 |
| 22.01.31 | D | 4 | 12 | 23 |
In df2 each location corresponds to one and only one date. I want to use this location to date map from df2, and apply a function to df1. This function should set x and y columns to zero (or na) for any date that comes after specified date for that location on df2.
So the output df3 would be like this:
df3
| date | location | location_id | y | x |
|---|---|---|---|---|
| 22.02.02 | A | 1 | ||
| 22.02.02 | B | 2 | 45 | 67 |
| 22.02.02 | C | 3 | ||
| 22.02.02 | D | 4 | ||
| 22.02.01 | A | 1 | 37 | 67 |
| 22.02.01 | B | 2 | 82 | 23 |
| 22.02.01 | C | 3 | ||
| 22.02.01 | D | 4 | ||
| 22.01.31 | A | 1 | 61 | 37 |
| 22.01.31 | B | 2 | 90 | 65 |
| 22.01.31 | C | 3 | ||
| 22.01.31 | D | 4 | 12 | 23 |
| 22.01.30 | A | 1 | 38 | 48 |
| 22.01.30 | B | 2 | 57 | 53 |
| 22.01.30 | C | 3 | 75 | 95 |
| 22.01.30 | D | 4 | 76 | 19 |
I do not know how to solve this kind of questions, what is the most efficient way to solve this, is it using dplyr or for loop?! I am approaching this with using dplyr ,groupby and mutate. Something Like this (I do not get result that I want).
Here my code which does not give me the result:
df1 %>%
group_by(location, id_location) %>%
mutate(date = df2$date, y= df2$y, x = df2$x)
I am new to R and using dplyr.