I have two tibbles that look like this:
1.
a <- tribble(
~id_emca, ~year, ~id_estmun.x, ~id_estmun.y,
"DLAGU20", 2006, 1005, 1005,
"DLAGU20", 2007, 1005, 1005,
"DLAGU20", 2008, 1005, 1005,
"DLBAJ155", 2012, 3003, 3001,
"DLBAJ155", 2013, 3003, 3001,
"DLBAJ157", 2017, 3007, 5010,
"DLBAJ157", 2018, 3007, 5010,
)
id_emca year id_estmun.x id_estmun.y
<chr> <dbl> <dbl> <dbl>
1 DLAGU20 2006 1005 1005
2 DLAGU20 2007 1005 1005
3 DLAGU20 2008 1005 1005
4 DLBAJ155 2012 3003 3001
5 DLBAJ155 2013 3003 3001
6 DLBAJ157 2017 3007 5010
7 DLBAJ157 2018 3007 5010
b <- tribble(
~start, ~end, ~id_estmun, ~Party,
2005, 2007, 1005, "PRI",
2008, 2010, 1005, "PAN",
2010, 2012, 3001, "PRI",
2013, 2015, 3001, "PT",
2010, 2012, 3003, "COAL",
2013, 2015, 3003, "PRD",
2016, 2018, 3007, "PAN",
2019, 2021, 3007, "COAL",
2016, 2018, 5010, "PRI",
)
start end id_estmun Party
<dbl> <dbl> <dbl> <chr>
1 2005 2007 1005 PRI
2 2008 2010 1005 PAN
3 2010 2012 3001 PRI
4 2013 2015 3001 PT
5 2010 2012 3003 COAL
6 2013 2015 3003 PRD
7 2016 2018 3007 PAN
8 2019 2021 3007 COAL
9 2016 2018 5010 PRI
I am trying to create a new tibble in such a way that if the id_estmun.x
and id_estmun.y
variables are equal to the id_estmun
on tibble 2 and the year
variable is equal or in between the start
and end
variables, the variable Party should be merged to the right side of the first tibble.
Here is an example of what the new tibble should look like, this should make everything more clear:
id_emca year id_estmun.x id_estmun.y Party.x Party.y
<chr> <dbl> <dbl> <dbl> <chr> <chr>
1 DLAGU20 2006 1005 1005 PRI PRI
2 DLAGU20 2007 1005 1005 PRI PRI
3 DLAGU20 2008 1005 1005 PAN PAN
4 DLBAJ155 2012 3003 3001 COAL PRI
5 DLBAJ155 2013 3003 3001 PRD PT
6 DLBAJ157 2017 3007 5010 PAN PRI
7 DLBAJ157 2018 3007 5010 PAN PRI
This is what I have tried and it seems to work, but I was wondering if there was an easier way to do it:
c <- a %>%
left_join(b,
by = c("id_estmun.x"= "id_estmun"),
keep = TRUE) %>%
filter(year >= start & year <= end) %>% rename(Party.x = Party) %>%
dplyr::select(-one_of(c("start", "end", "id_estmun")))
d <- c %>%
left_join(b,
by = c("id_estmun.y"= "id_estmun"),
keep = TRUE) %>%
filter(year >= start & year <= end) %>% rename(Party.y = Party) %>%
dplyr::select(-one_of(c("start", "end", "id_estmun")))