Having a data frame which provides a specific timestamp
dframe1 <- structure(list(id = c(1L, 1L, 1L, 2L, 2L), name = c("Google",
"Yahoo", "Amazon", "Amazon", "Google"), date = c("2008-11-01",
"2008-11-01", "2008-11-04", "2008-11-01", "2008-11-02")), class = "data.frame", row.names = c(NA,
-5L))
And a second one from which I would like to keep info before and after the specific time from the first dataframe
dframe2 <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L), date = c("2008-11-01", "2008-11-01",
"2008-11-04", "2008-10-31", "2008-10-31", "2008-11-02", "2008-11-02",
"2008-11-02", "2008-11-05", "2008-11-02", "2008-11-03", "2008-10-31",
"2008-11-01", "2008-11-01", "2008-11-02", "2008-11-02", "2008-11-03"
), text_sth = c("test", "text_sth", "text here", "another text",
"other", "another one", "test", "text_sth", "text here", "another text",
"other", "etc", "test", "text_sth", "text here", "another text",
"text here")), row.names = c(NA, -17L), class = "data.frame")
How is it possible to have this output?
id text_sth name label
1 another text other Google before
1 another one test text_sth another text Google after
1 another text other Yahoo before
1 another one test text_sth another text Yahoo after
1 other Amazon before
1 text here Amazon after
Using this code it keeps results only if it find the two days before and after timestamps. How is it possible to change it and make it to keep until the two days before and after even if the before and after timestamps does not exist but all the previous days until then exist? From here
left_join(dframe1, df2, by = "id") %>%
mutate(date_diff = as.numeric(date.y - date.x)) %>%
filter(abs(date_diff) == 2) %>%
mutate(label = ifelse(date_diff == -2, "before", "after")) %>%
select(id, name, label, text_sth)