So you're looking for a way to keep both sets of join keys in the output? This is currently only supported in nest_join() by way of the keep = TRUE argument. Once, you have both keys, creating the exacerbation variable is straightforward.
library(tidyverse)
#Example dataframe 1
df1 <- tibble(patientid = c("-2147483645", "-2147483645", "-2147483645", "-2147483645", "-2147483645"),
date = c("2018-08-30", "2018-09-04", "2018-09-13", "2018-09-28", "2018-10-11"))
#Example dataframe 2
df2 <- tibble(patientid = c("-2147483645", "-2147483645"),
date1 = c("2018-09-04", "2018-09-28"))
df1 %>%
nest_join(df2, by = c("patientid", "date" = "date1"), keep = TRUE) %>%
unnest(cols = df2, keep_empty = TRUE, names_repair = "universal") %>%
select(patientid = patientid...1, date, date1) %>%
mutate(exacerbation = if_else(is.na(date1), "NO", "YES"))
#> New names:
#> * patientid -> patientid...1
#> * patientid -> patientid...3
#> # A tibble: 5 x 4
#> patientid date date1 exacerbation
#> <chr> <chr> <chr> <chr>
#> 1 -2147483645 2018-08-30 <NA> NO
#> 2 -2147483645 2018-09-04 2018-09-04 YES
#> 3 -2147483645 2018-09-13 <NA> NO
#> 4 -2147483645 2018-09-28 2018-09-28 YES
#> 5 -2147483645 2018-10-11 <NA> NO
Created on 2020-04-29 by the reprex package (v0.3.0)
In dplyr 1.0.0, the keep parameter will be added to the outer joins after which this operation can be performed without have to resort to tidyr::unnest().