【发布时间】:2017-09-27 08:19:46
【问题描述】:
我有一个半周期测量的列表 - 这意味着它们应该在一个间隔内完成,但是有时会有 NA 并且测量将重新开始。
在另一个列表中,我有关于事件的信息。
对于每次测量,我想知道过去最后一次事件的日期。我如何在 R 中做到这一点,最好使用 dplyr?
library(dplyr)
library(lubridate)
measurements <- tibble(timestamp = seq(ymd('2017-01-01'),
ymd('2017-01-20'),
by = "2 days"),
data = runif(10))
events <- tibble(timestamp = ymd('2017-01-04', '2017-01-12'),
type = 'Start')
expected = ymd(NA, NA, '2017-01-04', '2017-01-04',
'2017-01-04', '2017-01-04',
'2017-01-12', '2017-01-12',
'2017-01-12', '2017-01-12')
measurements %>% mutate(distance = expected)
# A tibble: 10 x 3
timestamp data distance
<date> <dbl> <date>
1 2017-01-01 0.01037106 NA
2 2017-01-03 0.50183512 NA
3 2017-01-05 0.80695523 2017-01-04
4 2017-01-07 0.98605880 2017-01-04
5 2017-01-09 0.78591144 2017-01-04
6 2017-01-11 0.02296494 2017-01-04
7 2017-01-13 0.94335407 2017-01-12
8 2017-01-15 0.10540759 2017-01-12
9 2017-01-17 0.27344290 2017-01-12
10 2017-01-19 0.09080328 2017-01-12
【问题讨论】:
-
不完全确定“最后一个”是什么意思。假设您想要 2017-1-1 的距离为 2017-01-04,那么这将起作用:
measurements %>% mutate(distance = expected) -> d; d %>% group_by(distance) %>% summarise(min(timestamp)). -
其实
expected是OP想要的o/p。条件是measurements$timestamp必须与events$timestamp进行比较,并且最近/最后日期必须应用于距离。 -
我只想计算变量“预期”而不是输入它。
-
乍一看,这看起来像是一个滚动连接的案例,目前是not supported by
dplyr。不过,data.table应该很简单。