Наличие фрейма данных, который предоставляет определенную временную метку
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))
И второй, из которого я хотел бы сохранить информацию до и после определенного времени с первого фрейма данных
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")
Как можно получить этот вывод?
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
Используя этот код, он сохраняет результаты, только если он находит два дня до и после отметок времени. Как можно изменить его и сохранить до двух дней до и после, даже если отметки времени до и после не существуют, но существуют все предыдущие дни до этого? С здесь
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)