r - 可以检查时间(无日期)是否在特定的日期时间间隔内?
问题描述
示例:我想检查 03:00 是否在 2015-01-05 00:52 和 2015-01-05 05:52 的区间内,对于这种情况,我想要的结果是 TRUE。
提前致谢
解决方案
我为具有多个最大和最小日期时间的数据框提出了此解决方案(解决方案比创建可重现示例的行数短):
# Reproducible example
set.seed(666)
timeLimits <- seq(from = (as.POSIXct("1996-6-6 06:06:06")),
to = (as.POSIXct("1997-6-6 06:06:06")),
by = "hour")
timeLimits <- matrix(data = sort(sample(x = timeLimits, size = 66)),
ncol = 2, byrow = TRUE)
timeLimits <- data.frame(min = as.POSIXct(x = timeLimits[,1], origin = "1970-1-1 00:00:00"),
max = as.POSIXct(x = timeLimits[,2], origin = "1970-1-1 00:00:00"))
# The solution
require(lubridate)
answer <- apply(timeLimits, 2, as.character)
answer <- apply(timeLimits, 1, function(x) is.element(3, hour(seq(as.POSIXct(x[1]), as.POSIXct(x[2]), "hour"))))
推荐阅读
- google-cloud-platform - (gcloud.dataflow.flex-template.build) PERMISSION_DENIED:调用者没有权限
- gitlab-ci - 在 gitlab-ci 中触发管道失败
- java - 在横向模式下无法隐藏导航栏
- django - Django创建嵌套模型
- postgresql - 无法在postgres中使用spring数据jdbc保存实体
- php - 当通过 PHP 动态提供 CSS 时,为什么我会在 Edge 中遇到内容类型兼容性问题?
- java - 如何更新片段中的 recyclerview 列表?(notifyDataSetChanged 不起作用)
- python - Python matplotlib 每个级别都有不同的颜色
- apache - Here-api Autosuggest 通过服务器代理时偶尔会返回 401 Unauthorized
- r - 使用 LpSolveAPI 赋值,如何以类似广度优先的方式填充参数?