使用相關問題中的示例:Rearly month end in R
library(lubridate)
library(dplyr)
dt<-data.frame(orig_dt=as.Date(c("1997-04-01","1997-06-29")))
dt %>% mutate(round_dt=round_date(orig_dt, unit="month"),
modified_dt=round_date(orig_dt, unit="month")-days(1))
在一個會話中,我正確地獲得了四舍五入的日期(R 4.0.0,Rcpp_1.0.4.6 通過命名空間加載)
orig_dt round_dt modified_dt
1 1997-04-01 1997-04-01 1997-03-31
2 1997-06-29 1997-07-01 1997-06-30
在另一個會話中,我得到了發言權而不是回合(不同的機器,R 4.0.2,Rcpp 未通過命名空間加載)
orig_dt round_dt modified_dt
1 1997-04-01 1997-04-01 1997-03-31
2 1997-06-29 1997-06-01 1997-05-31
我認為這可能與 Rcpp 有關,因為之前我收到一條錯誤訊息
Error in C_valid_tz(tzone) (rscrpt.R#27): function 'Rcpp_precious_remove' not provided by package 'Rcpp'
Show stack trace
雖然我不再收到錯誤,但值不同,我想知道為什么/如何在不完全重新安裝的情況下修復它。
uj5u.com熱心網友回復:
我能夠在 vanilla R 會話中重現您的問題。
$ R --vanilla
> R.version.string
[1] "R version 4.1.2 (2021-11-01)"
> packageVersion("lubridate")
[1] ‘1.8.0’
> library("lubridate")
> round_date(ymd("1997-06-29"), unit = "month")
[1] "1997-06-01"
這似乎是一個錯誤round_date,在此提交中引入。在提交之前,round_date包含的正文:
above <- unclass(as.POSIXct(ceiling_date(x, unit = unit, week_start = week_start)))
mid <- unclass(as.POSIXct(x))
below <- unclass(as.POSIXct(floor_date(x, unit = unit, week_start = week_start)))
這里x = ymd("1997-06-29"),unit = "month"和below,mid以及above被定義為秒數從1970-01-01 00:00:00 UTC以月樓x,x以及一個月的上限x,分別為(更準確地說,時間00:00:00上這三個日期,在你的系統的時區)。因此,below < mid < above和round_date將比較mid-below以above-mid確定哪個below和above更接近mid。
自提交以來,mid已定義為
mid <- unclass(x)
這是從到的天數。現在,,使消極和積極。其結果是,認為是“接近”比,它錯誤地舍入到。1970-01-01xmid << below < abovemid-belowabove-midround_datebelowmidabove1997-06-291997-06-01
我已在此處向軟體包維護人員報告了該問題。我想它很快就會修復......
同時,您可以嘗試lubridate從提交之前恢復到舊版本的,或使用以下臨時解決方法:
round_date_patched <- function(x, unit) {
as.Date(round_date(as.POSIXct(x), unit = unit))
}
round_date_patched(ymd("1997-06-29"), unit = "month") # "1997-07-01"
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/374931.html
上一篇:如何選擇特定日期之前訂單的客戶
