gpt4 book ai didi

r - 提取润滑间隔内的月份

转载 作者:行者123 更新时间:2023-12-02 17:14:58 25 4
gpt4 key购买 nike

给定一个润滑间隔,例如:

start <- "2016-09-24"
finish <- "2016-11-02"
my_interval <- lubridate::interval(start, finish)
my_interval

> my_interval
[1] 2016-09-24 UTC--2016-11-02 UTC

我希望能够提取属于这个区间的月份,在这种情况下:

[1] "Sep" "Oct" "Nov"

到目前为止,我在这方面的最佳尝试真的很笨拙:

my_months <- list(
"Aug" = interval("2016-08-01", "2016-08-31"),
"Sep" = interval("2016-09-01", "2016-09-30"),
"Oct" = interval("2016-10-01", "2016-10-31"),
"Nov" = interval("2016-11-01", "2016-11-30"),
"Dec" = interval("2016-12-01", "2016-12-31")
)

extract_months <- function(x, months) {
out <- vector(mode = "character")

for (i in seq_along(months)) {
in_month <- int_overlaps(x, months[[i]])
if (in_month) {
out[i] <- names(months)[i]
}
out <- out[!is.na(out)]
}
out
}

extract_months(x = my_interval, months = my_months)

> extract_months(x = my_interval, months = my_months)
[1] "Sep" "Oct" "Nov"

多年来,这很快变得笨拙。我希望有人有更好的解决方案。

我看不出这个问题与 Subset a dataframe between 2 dates 的重复

最佳答案

其实很简单!

library(lubridate)
month.abb[month(start):month(finish)]

如果这不起作用,请告诉我。

关于r - 提取润滑间隔内的月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46898837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com