如何在datetime对象中处理一个含糊不清的世纪? [英] How to handle an ambiguous century in datetime objects?
问题描述
vals< - as.character(60:70)
as.POSIXct(vals,origin =1900-01-01,format =%y)
#[1]2060-07-25 EDT2061-07-25 EDT2062-07-25 EDT2063 -07-25 EDT
#[5]2064-07-25 EDT2065-07-25 EDT2066-07-25 EDT2067-07-25 EDT
#[9]2068-07-25 EDT1969-07-25 EDT1970-07-25 EDT
是否可以调整原点,以便 as.POSIXct
将返回1960输入60?处理一个歧义世纪的最好方法是什么?
你不能使 as.POSIXct
返回1960,输入60
。请参阅?strptime
:
'%y'年份没有世纪-99)。在输入上,值00到68的值为
,前缀为20和69到99之间的19 - 即2004和2008年POSIX标准规定的行为
,但是它们的
也表示为预计在未来版本中,从2位数年份推断的默认
世纪将会改变。
您需要在世纪前加上字符串,并使用%Y
格式,如果你想要不同的行为与 as.POSIXct
。
vals< - as.character(60:70)
as.POSIXct(paste0(19,vals),format =%Y)
如果两位数之后的某些日期是2000年后,您可以使用 ifelse
或类似于前面不同世纪的东西。
newvals< - paste0 ifelse(vals<20,20,19),vals)
I am playing around with datetime stuff in R and cannot figure out how to alter the time origin to accept older dates. For example:
vals <- as.character(60:70)
as.POSIXct(vals, origin="1900-01-01", format = "%y")
# [1] "2060-07-25 EDT" "2061-07-25 EDT" "2062-07-25 EDT" "2063-07-25 EDT"
# [5] "2064-07-25 EDT" "2065-07-25 EDT" "2066-07-25 EDT" "2067-07-25 EDT"
# [9] "2068-07-25 EDT" "1969-07-25 EDT" "1970-07-25 EDT"
Is it possible to adjust the origin such that as.POSIXct
will return 1960 for an input of "60"? What is the best way to handle an ambiguous century?
You can't make as.POSIXct
return 1960 for an input of "60"
. See ?strptime
:
‘%y’ Year without century (00-99). On input, values 00 to 68 are
prefixed by 20 and 69 to 99 by 19 - that is the behaviour
specified by the 2004 and 2008 POSIX standards, but they do
also say ‘it is expected that in a future version the default
century inferred from a 2-digit year will change’.
You need to prepend the century to the string and use the "%Y"
format if you want different behavior with as.POSIXct
.
vals <- as.character(60:70)
as.POSIXct(paste0("19",vals), format = "%Y")
If some of the two-digit dates are after 2000, you can use ifelse
or something similar to prepend a different century.
newvals <- paste0(ifelse(vals < "20", "20", "19"), vals)
这篇关于如何在datetime对象中处理一个含糊不清的世纪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!