【发布时间】:2017-12-08 20:45:49
【问题描述】:
我正在尝试确定日期时间是否介于开始日期时间和结束日期时间之间,以及它是否返回与此匹配的值。它在 data.table 中工作,但想让它在 DPLYR 中工作。
所以如果你有日期时间:
2017-07-01 02:15:00
2017-07-01 02:30:00
在第二张表中查找这些
begin, end, value1, value2
2017-07-01 00:01:00, 2017-07-01 01:00:00, 1, 2
2017-07-01 01:01:00, 2017-07-01 02:00:00, 3, 4
2017-07-01 02:01:00, 2017-07-01 03:00:00, 5, 6
返回
date value1 value2
2017-07-01 02:15:00 5 6
2017-07-01 02:30:00 5 6
有许多查找值,因此查找日期时间可能有几百个。
我有这个使用 data.table 但想使用 DPLYR 来减少对许多包的依赖。这是我目前所拥有的:
library(tidyverse)
library(lubridate)
library(data.table)
dates <- read_csv("date1.csv") %>%
mutate(date = as_datetime(date))
lookup <- read_csv("lookup.csv") %>%
mutate(begin = as_datetime(begin),
end = as_datetime(end))
dates <- data.table(dates)
lookup <- data.table(lookup)
setkey(lookup, begin, end)
dates[, c("begin", "end") := date]
test.df <- foverlaps(dates, lookup)[, c("date", "value1", "value2"),
with = FALSE]
我正在考虑使用类似的东西:
test <- dates %>% rowwise() %>%
mutate(value1 = ifelse( lookup$begin >= date & date <= lookup$end, lookup$value1, ""))
这是要查找的日期:
dates <- structure(list(date = structure(c(1498867200, 1498868100, 1498869000,
1498869900, 1498870800, 1498871700, 1498872600, 1498873500, 1498874400,
1498875300, 1498876200, 1498877100, 1498878000, 1498878900, 1498879800,
1498880700, 1498881600, 1498882500), tzone = "UTC", class = c("POSIXct",
"POSIXt"))), .Names = "date", class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -18L))
查找表:
lookup <- structure(list(begin = structure(c(1498867260, 1498870860, 1498874460,
1498878060, 1498881660, 1498885260, 1498888860, 1498892460, 1498896060
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), end = structure(c(1498870800,
1498874400, 1498878000, 1498881600, 1498885200, 1498888800, 1498892400,
1498896000, 1498899600), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
value1 = c(1L, 3L, 5L, 7L, 9L, 11L, 13L, 15L, 17L), value2 = c(2L,
4L, 6L, 8L, 10L, 12L, 14L, 16L, 18L)), .Names = c("begin",
"end", "value1", "value2"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -9L))
【问题讨论】:
-
在
data.table中使用非等连接代替foverlaps,而做操作rowwise也不能代替它。 -
嗨,谢谢,这是选项二,但我想为此使用 DPLYR。我想到的一个选项是将数据框切换为长格式,将其加入每 15 分钟的日期时间序列(因为所有数据都在四分之一小时内),然后填写值,然后再进行一次连接。这是可能的,但即使对于我的业余编码来说,它似乎也很复杂。
标签: r dplyr data.table lubridate