英文:
Calculate Julian Day based on a different reference day
问题
我的目的是基于一个新的参考日期来计算儒略日,也就是说,现在儒略日1对应6月1日,儒略日366对应1月1日。
大家好,
我一直在尝试计算儒略日,其中参考日期不再是1月1日。事实上,我想从我的数据中提取儒略日,其中6月1日对应1,而1月1日对应366。
我尝试将以1月1日为参考的儒略日加上152,但我的数据超过了366,我认为这不是精确的解决方法。
我真的很困惑,知道这涉及到数学序列,但我不太熟悉。
如果你能帮助我,我将非常感激
英文:
My purpose is to calculate Julian Day based on a new reference, meaning that Julian Day 1 now will correspond to June 1st and Julian Day 366 to January 1st.
Hello everyone,
I've been struggling to calculate Julian Day where the reference day is not January 1st anymore.
Indeed, I want to extract from my data Julian Day where June 1st correspond to 1 and January 1st to 366.
I tried to add 152 to Julian Day calculated with January 1st as a reference however my data than exceed 366 and I don't think it's the exact solution.
I'm really confused and know it's about mathematical suite but I'm not comfortable with it.
If you could help me I would be very grateful
答案1
得分: 1
在假设您指的是5月31日
而不是1月1日
的情况下,可以使用以下可能有点复杂的方法开始:
set.seed(42)
dates <- sort(as.Date("2020-06-01") + c(-1, 0, sample(1000, size=5)))
dates
# [1] "2020-05-31" "2020-06-01" "2020-08-14" "2020-11-01" "2021-04-18" "2021-12-14" "2023-02-23"
june1s <- as.POSIXlt(paste0(substring(dates, 1, 4), "-06-01"))
june1s
# [1] "2020-06-01 EDT" "2020-06-01 EDT" "2020-06-01 EDT" "2020-06-01 EDT" "2021-06-01 EDT" "2021-06-01 EDT"
# [7] "2023-06-01 EDT"
june1s$year <- june1s$year - (as.Date(june1s) > dates)
dates - as.Date(june1s) + 1L
# Time differences in days
# [1] 366 1 75 154 322 197 268
as.numeric(dates - as.Date(june1s) + 1L, units = "days")
# [1] 366 1 75 154 322 197 268
我包括了05-31
和06-01
,以演示以6月1日为基础的儒略年的两个极端。最后一步(as.numeric
)主要是我的一个防御性习惯:difftime
和类似的函数经常会根据差异的规模自动更改单位,这可能会有问题;使用units="days"
可以强制指定单位。(也有其他处理方式。)
您可以使用以下函数化这个过程:
myjulian <- function(dates, origin = "-01-01") {
if (!inherits(dates, "Date")) dates <- as.Date(dates)
origins <- as.POSIXlt(paste0(substring(dates, 1, 4), origin))
origins$year <- origins$year - (as.Date(origins) > dates)
as.numeric(dates - as.Date(origins) + 1L, units = "days")
}
并进行简单的验证:
myjulian("2023-06-01") # 以1月1日为基准
# [1] 152
format(as.Date("2023-06-01"), format = "%j")
# [1] "152"
myjulian("2023-06-01", origin = "-06-01")
# [1] 1
英文:
Under the assumption that you meant May 31
instead of Jan 1
, then a perhaps-convoluted method could start with:
set.seed(42)
dates <- sort(as.Date("2020-06-01") + c(-1, 0, sample(1000, size=5)))
dates
# [1] "2020-05-31" "2020-06-01" "2020-08-14" "2020-11-01" "2021-04-18" "2021-12-14" "2023-02-23"
june1s <- as.POSIXlt(paste0(substring(dates, 1, 4), "-06-01"))
june1s
# [1] "2020-06-01 EDT" "2020-06-01 EDT" "2020-06-01 EDT" "2020-06-01 EDT" "2021-06-01 EDT" "2021-06-01 EDT"
# [7] "2023-06-01 EDT"
june1s$year <- june1s$year - (as.Date(june1s) > dates)
dates - as.Date(june1s) + 1L
# Time differences in days
# [1] 366 1 75 154 322 197 268
as.numeric(dates - as.Date(june1s) + 1L, units = "days")
# [1] 366 1 75 154 322 197 268
I included 05-31
and 06-01
to demonstrate the two extremes of the June-1-based-julian-year. The last step (as.numeric
) is mostly a defensive habit of mine: difftime
and similar functions often auto-change the units based on the scale of the difference, and that might be problematic; using units="days"
forces this. (There are other ways to deal with this, as well.)
One can functionize this with:
myjulian <- function(dates, origin = "-01-01") {
if (!inherits(dates, "Date")) dates <- as.Date(dates)
origins <- as.POSIXlt(paste0(substring(dates, 1, 4), origin))
origins$year <- origins$year - (as.Date(origins) > dates)
as.numeric(dates - as.Date(origins) + 1L, units = "days")
}
With brief validation:
myjulian("2023-06-01") # jan-1
# [1] 152
format(as.Date("2023-06-01"), format = "%j")
# [1] "152"
myjulian("2023-06-01", origin = "-06-01")
# [1] 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论