如何用动物园阅读“14-OCT-2016”或“%dd-%mon-%YYYY”? r/动物园/xts

How to read "14-OCT-2016" or "%dd-%mon-%YYYY" with zoo? r / zoo/ xts

标题不言自明。我有一个 df,第一列一直是 "14-OCT-2016" 运行。我需要将此 df 读为 .xts

我看了:

df$NAV.Date <- as.Date(df$NAV.Date, "%Y %b %d" )  # this replaces all values with NA

df$NAV.Date <- strftime(test$NAV.Date, format="%Y %b %d" )

我得到的最后一个:

Error in as.POSIXlt.character(as.character(x), ...) : 
  character string is not in a standard unambiguous format

提前致谢。

这是系统区域设置问题(很有可能)。解决方法如下:

test<-"14-OCT-2016"
lct<-Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME","C") #moves you to the right locale
date<-as.Date(test, "%d-%b-%Y")
print(date)
Sys.setlocale("LC_TIME",lct) #moves you back again

希望对你有所帮助:D

对于第二个问题,你必须使用lubridate代替

library(lubridate)
parse_date_time("12-OCT-2015", tz="UTC", c("dbY", "dby"), locale = "C")
parse_date_time("12-OCT-15", tz="UTC", c("dbY", "dby"), locale = "C")

在线解决方案如下,借助包anytime:

test <- "14-OCT-2016"
date <- anytime::anydate(test)
#[1] "2016-10-14"

第二个问题可以用lubridate解决:

test2 <- "16-Feb-15"
date2 <- lubridate::dmy(test2)
#[1] "2015-02-16"