如何查看一个向量中的值是否位于另一向量中的任意两个值之间

发布于 2025-01-09 13:11:31 字数 583 浏览 1 评论 0原文

我有两个日期时间格式的向量,我想知道向量 A 中的任何日期时间戳是否落在向量 B 中的两个给定日期时间戳之间。

data <- tibble(
  Col_A = structure(c(1514935860, 1514936280, 1514946120, 1515090600, 1515090600, 1515095040), tzone = "UTC", class = c("POSIXct", "POSIXt")),
  Col_B = structure(c(1517564040, 1517564340, 1517564640, 1517564940, 1517565240, 1517565540), tzone = "UTC", class = c("POSIXct", "POSIXt"))

我已经尝试过此操作,

check <- data$Col_A
data$dummy <- ifelse(data$Col_B %in% check, "1", "0")

但这仅返回Col_A 中的数据点在 Col_B 的整个范围内,但我需要代码来检查 Col_B 中两个日期时间戳的每个间隔。

I have two vectors in date time format, and I would like to know if any of the date time stamps in vector A fall in between any of the two given date time stamps in vector B.

data <- tibble(
  Col_A = structure(c(1514935860, 1514936280, 1514946120, 1515090600, 1515090600, 1515095040), tzone = "UTC", class = c("POSIXct", "POSIXt")),
  Col_B = structure(c(1517564040, 1517564340, 1517564640, 1517564940, 1517565240, 1517565540), tzone = "UTC", class = c("POSIXct", "POSIXt"))

I have tried this,

check <- data$Col_A
data$dummy <- ifelse(data$Col_B %in% check, "1", "0")

but this only returns the data points in Col_A within the entire range of Col_B, but I need the code to check every interval of two date time stamps in Col_B.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住我的手 2025-01-16 13:11:31

如果我们需要对任何组合、

data$check <- combn(data$Col_B, 2, FUN = function(x) 
     +(any(dplyr::between(data$Col_A, x[1], x[2]))))

数据执行此操作

data <- structure(list(Col_A = structure(c(1514935860, 1514936280, 1514946120, 
1515090600, 1515090600, 1515095040), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Col_B = structure(c(1517564040, 1517564340, 1517564640, 
1517564940, 1517565240, 1517565540), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L))

If we need to do this on any combination,

data$check <- combn(data$Col_B, 2, FUN = function(x) 
     +(any(dplyr::between(data$Col_A, x[1], x[2]))))

data

data <- structure(list(Col_A = structure(c(1514935860, 1514936280, 1514946120, 
1515090600, 1515090600, 1515095040), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Col_B = structure(c(1517564040, 1517564340, 1517564640, 
1517564940, 1517565240, 1517565540), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文