首页 > 解决方案 > 使用来自另一个数据帧的 id 和时间戳创建特定的输出

问题描述

有这样的数据框:

dfirst <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), 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"), text_sth = c("test", "text_sth", 
"text here", "another text", "other", "another one", "test", 
"text_sth", "text here", "another text", "other")), class = "data.frame", row.names = c(NA, 
-11L))

像这样的第二个数据框:

dsecond <- structure(list(id = c(1L, 1L, 1L), name = c("Google", "Yahoo", 
"Amazon"), date = c("2008-11-01", "2008-11-01", "2008-11-04")), class = "data.frame", row.names = c(NA, 
-3L))

如何创建一个输出,其中包含数据框 1 数据中的第二个数据过滤器中记录的前一个和后日期

id       date     text_sth   name label
1 2008-10-31 another text Google   pre
1 2008-10-31        other Google   pre
1 2008-11-02  another one Google after
1 2008-11-02         test Google after
1 2008-11-02     text_sth Google after
1 2008-11-02 another text Google after
1 2008-10-31 another text  Yahoo   pre
1 2008-10-31        other  Yahoo   pre
1 2008-11-02  another one  Yahoo after
1 2008-11-02         test  Yahoo after
1 2008-11-02     text_sth  Yahoo after
1 2008-11-02 another text  Yahoo after
1 2008-11-03        other Amazon   pre
1 2008-11-05    text here Amazon after

标签: r

解决方案


推荐阅读