anytime 包中的格式问题
Issue with the format in anytime package
library("anytime")
这个效果很好:
anytime("08/24/2014 01:28:00")
anytime("2014/08/24 01:28:00")
[1] "2014-08-24 00:28:00 NZST"
这不是:
anytime("24/08/2014 01:28:00")
anytime("2014/24/08 01:28:00")
[1] NA
这是什么原因,选项是什么(仅使用 anytime 包)?
如果格式不在默认格式列表中,我们可以在 anytime
中添加带有 addFormats
的格式,方法是使用
getFormats()
#[1] "%Y-%m-%d %H:%M:%S%f" "%Y/%m/%d %H:%M:%S%f" "%Y%m%d %H%M%S%f"
#[4] "%Y%m%d %H:%M:%S%f" "%m/%d/%Y %H:%M:%S%f" "%m-%d-%Y %H:%M:%S%f"
#[7] "%Y-%b-%d %H:%M:%S%f" "%Y/%b/%d %H:%M:%S%f" "%Y%b%d %H%M%S%F"
#[10] "%Y%b%d %H:%M:%S%F" "%b/%d/%Y %H:%M:%S%f" "%b-%d-%Y %H:%M:%S%f"
#[13] "%d.%b.%Y %H:%M:%S%f" "%Y-%B-%d %H:%M:%S%f" "%Y/%B/%d %H:%M:%S%f"
#[16] "%Y%B%d %H%M%S%f" "%Y%B%d %H:%M:%S%f" "%B/%d/%Y %H:%M:%S%f"
#[19] "%B-%d-%Y %H:%M:%S%f" "%d.%B.%Y %H:%M:%S%f" "%a %b %d %H:%M:%S%F %Y"
#[22] "%Y-%m-%d" "%Y%m%d" "%m/%d/%Y"
#[25] "%m-%d-%Y" "%Y-%b-%d" "%Y%b%d"
#[28] "%b/%d/%Y" "%b-%d-%Y" "%Y-%B-%d"
#[31] "%Y%B%d" "%B/%d/%Y" "%B-%d-%Y"
如果我们检查给出 NA 的 format
,它不在 getFormats
列表中
c("%d/%m/%Y %H:%M:%S", "%Y/%d/%m %H:%M:%S") %in% getFormats()
#[1] FALSE FALSE
因此,我们可以使用 addFormats
添加格式并应用 anytime
anytime::addFormats(c("%d/%m/%Y %H:%M:%S", "%Y/%d/%m %H:%M:%S"))
anytime("24/08/2014 01:28:00")
#[1] "2014-08-24 01:28:00 IST"
anytime("2014/24/08 01:28:00")
#[1] "2014-08-24 01:28:00 IST"
更新
最新版本anytime
,getFormats()
有41种格式
length(getFormats())
#[1] 41
但是,OP 的 post 中指定的格式仍未包括在内,必须遵循 addFormats
路线
library("anytime")
这个效果很好:
anytime("08/24/2014 01:28:00")
anytime("2014/08/24 01:28:00")
[1] "2014-08-24 00:28:00 NZST"
这不是:
anytime("24/08/2014 01:28:00")
anytime("2014/24/08 01:28:00")
[1] NA
这是什么原因,选项是什么(仅使用 anytime 包)?
如果格式不在默认格式列表中,我们可以在 anytime
中添加带有 addFormats
的格式,方法是使用
getFormats()
#[1] "%Y-%m-%d %H:%M:%S%f" "%Y/%m/%d %H:%M:%S%f" "%Y%m%d %H%M%S%f"
#[4] "%Y%m%d %H:%M:%S%f" "%m/%d/%Y %H:%M:%S%f" "%m-%d-%Y %H:%M:%S%f"
#[7] "%Y-%b-%d %H:%M:%S%f" "%Y/%b/%d %H:%M:%S%f" "%Y%b%d %H%M%S%F"
#[10] "%Y%b%d %H:%M:%S%F" "%b/%d/%Y %H:%M:%S%f" "%b-%d-%Y %H:%M:%S%f"
#[13] "%d.%b.%Y %H:%M:%S%f" "%Y-%B-%d %H:%M:%S%f" "%Y/%B/%d %H:%M:%S%f"
#[16] "%Y%B%d %H%M%S%f" "%Y%B%d %H:%M:%S%f" "%B/%d/%Y %H:%M:%S%f"
#[19] "%B-%d-%Y %H:%M:%S%f" "%d.%B.%Y %H:%M:%S%f" "%a %b %d %H:%M:%S%F %Y"
#[22] "%Y-%m-%d" "%Y%m%d" "%m/%d/%Y"
#[25] "%m-%d-%Y" "%Y-%b-%d" "%Y%b%d"
#[28] "%b/%d/%Y" "%b-%d-%Y" "%Y-%B-%d"
#[31] "%Y%B%d" "%B/%d/%Y" "%B-%d-%Y"
如果我们检查给出 NA 的 format
,它不在 getFormats
列表中
c("%d/%m/%Y %H:%M:%S", "%Y/%d/%m %H:%M:%S") %in% getFormats()
#[1] FALSE FALSE
因此,我们可以使用 addFormats
添加格式并应用 anytime
anytime::addFormats(c("%d/%m/%Y %H:%M:%S", "%Y/%d/%m %H:%M:%S"))
anytime("24/08/2014 01:28:00")
#[1] "2014-08-24 01:28:00 IST"
anytime("2014/24/08 01:28:00")
#[1] "2014-08-24 01:28:00 IST"
更新
最新版本anytime
,getFormats()
length(getFormats())
#[1] 41
但是,OP 的 post 中指定的格式仍未包括在内,必须遵循 addFormats
路线