为什么 lubridate::mdy() 在我的输入中缺少日期时添加日期?
Why does lubridate::mdy() add day when it is missing from my input?
我故意在这段代码中省略了日期,并希望它失败,return 警告,或 return 不完整的记录。
txt <- "January 2010"
lubridate::mdy(txt)
输出为“2010-01-20”。为什么我输入的日期不是“20”?该值背后的逻辑是什么?
与解析顺序有关。根据?mdy
In case of heterogeneous date formats, the ymd() family guesses formats based on a subset of the input vector. If the input vector contains many missing values or non-date strings, the subset might not contain meaningful dates
原始字符串包括月份和 4 位年份,mdy
是 month
,day
year
和 year
可以是 2 位或4位数。现在,有一个混乱,它选择 2 位数字年份作为“10”,而日期被解析为“20”。相反,如果我们添加一天然后使用 mdy
,它将解析为 4 位数年份
lubridate::myd(paste(txt, '01'))
#[1] "2010-01-01"
我故意在这段代码中省略了日期,并希望它失败,return 警告,或 return 不完整的记录。
txt <- "January 2010"
lubridate::mdy(txt)
输出为“2010-01-20”。为什么我输入的日期不是“20”?该值背后的逻辑是什么?
与解析顺序有关。根据?mdy
In case of heterogeneous date formats, the ymd() family guesses formats based on a subset of the input vector. If the input vector contains many missing values or non-date strings, the subset might not contain meaningful dates
原始字符串包括月份和 4 位年份,mdy
是 month
,day
year
和 year
可以是 2 位或4位数。现在,有一个混乱,它选择 2 位数字年份作为“10”,而日期被解析为“20”。相反,如果我们添加一天然后使用 mdy
,它将解析为 4 位数年份
lubridate::myd(paste(txt, '01'))
#[1] "2010-01-01"