I try to read the date like 03-Nov-11, and I use the code as followed:
s<-c("03-Nov-13")
s1<-as.Date(s,"%d-%b-%y")
but s1 is NA.
The %b seems to be problematic indeed. I've had luck with those though:
s1 <- "03-11-13"
as.POSIXct(s1, format = "%d-%m-%y")
# [1] "2013-11-03 EDT"
s2 <- "03-NOVEMBRE-13" # Note that I have Fr locale, hence the ending in "BRE"
as.POSIXct(s2, format = "%d-%B-%y")
# [1] "2013-11-03 EDT"
as.POSIXct(s2, format = "%d-%b-%y")
# [1] "2013-11-03 EDT"
However, the abbreviated version doesn't seem to work at all on Windows:
s3 <- "03-NOV-13"
as.POSIXct(s3, format = "%d-%B-%y")
# [1] NA
as.POSIXct(s3, format = "%d-%b-%y")
# [1] NA
EDIT
After trying on Linux, the %b does what is expected!
as.POSIXct(s3, format="%d-%b-%y")
# [1] "2013-11-03 PDT"
as.POSIXct(s3, format="%d-%B-%y")
# [1] "2013-11-03 PDT"
EDIT 2
Filed a bug report;
See https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16301
EDIT 3
On my end I was mistaken -- my locale's months abbreviations are given by:
z <- seq.Date(as.Date('2015-01-01'), by='month', len = 12)
format(z, "%d-%b-%y")
# [1] "01-janv.-15" "01-févr.-15" "01-mars-15" "01-avr.-15" "01-mai-15"
# [6] "01-juin-15" "01-juil.-15" "01-août-15" "01-sept.-15" "01-oct.-15"
# [11] "01-nov.-15" "01-déc.-15"
So using "nov.", "NOV.", "NOVEMBRE" or "novembre" works fine.
Related
My session is in Spanish. When I have a format with full month name (%B) I get the correct date:
as.Date("01-Febrero-2021", format = "%d-%B-%Y")
# [1] "2021-02-01"
However, when I try to use the abbreviated month (%b), I get an "NA":
as.Date("01-Feb-2021", format = "%d-%b-%Y")
# [1] NA
as.Date("01-feb-2021", format = "%d-%b-%Y")
# [1] NA
as.Date("01-FEB-2021", format = "%d-%b-%Y")
# [1] NA
What I'm doing wrong?
Thanks to the answer from G. Grothendieck, I could make it work:
Sys.getlocale("LC_TIME")
# [1] "Spanish_Argentina.1252"
When checking the abbreviated month using format on Sys.Date, it turns out that the month is written with a period:
format(Sys.Date(), "%b")
# [1] "feb."
Try to parse a string, this time with a period:
as.Date("01-feb.-2021", format = "%d-%b-%Y")
# [1] "2021-02-01"
Works!
It works for me. Make sure you are actually in a Spanish locale. (I am on Windows and it is possible that it works differently on other platforms.)
Sys.setlocale("LC_TIME", "English")
## [1] "English_United States.1252"
Sys.getlocale("LC_TIME")
## [1] "English_United States.1252"
format(Sys.Date(), "%b")
## [1] "Feb"
Now change to Spanish:
Sys.setlocale("LC_TIME", "Spanish")
## [1] "Spanish_Spain.1252"
Sys.getlocale("LC_TIME")
## [1] "Spanish_Spain.1252"
format(Sys.Date(), "%b")
## [1] "feb"
as.Date("01-feb-2021", format="%d-%b-%Y")
## [1] "2021-02-01"
as.Date("01-Feb-2021", format="%d-%b-%Y")
## [1] "2021-02-01"
How do I use strptime or any other functions to parse time stamps with milliseconds in R?
time[1]
# [1] "2010-01-15 13:55:23.975"
strptime(time[1], format="%Y-%m-%d %H:%M:%S.%f")
# [1] NA
strptime(time[1], format="%Y-%m-%d %H:%M:%S")
# [1] "2010-01-15 13:55:23"`
Courtesy of the ?strptime help file (with the example changed to your value):
> z <- strptime("2010-01-15 13:55:23.975", "%Y-%m-%d %H:%M:%OS")
> z # prints without fractional seconds
[1] "2010-01-15 13:55:23 UTC"
> op <- options(digits.secs=3)
> z
[1] "2010-01-15 13:55:23.975 UTC"
> options(op) #reset options
You can also use strptime(time[1], "%OSn") where 0 <= n <= 6, without having to set digits.secs.
The documentation states "Which of these are supported is OS-dependent." so YMMV.
I want to get subseconds so I use following:
> options(digits.secs=6)
> as.POSIXlt(df1$Global.Time[5]/1000, origin="1970-01-01", tz="America/Los_Angeles")
[1] "2005-06-15 07:53:42.7 PDT"
Why does the output not contain something like "07:53:42.700000"?
Same problem with POSIXct:
> as.POSIXct(df1$Global.Time[3]/1000, origin="1970-01-01", tz="America/Los_Angeles")
[1] "2005-06-15 07:53:42.5 PDT"
How about this (corrected per Frank's direction):
d <- as.POSIXct(Sys.time())
format(d,"%Y-%m-%d %H:%M:%OS6")
[1] "2015-05-30 18:06:08.693852"
How do I use strptime or any other functions to parse time stamps with milliseconds in R?
time[1]
# [1] "2010-01-15 13:55:23.975"
strptime(time[1], format="%Y-%m-%d %H:%M:%S.%f")
# [1] NA
strptime(time[1], format="%Y-%m-%d %H:%M:%S")
# [1] "2010-01-15 13:55:23"`
Courtesy of the ?strptime help file (with the example changed to your value):
> z <- strptime("2010-01-15 13:55:23.975", "%Y-%m-%d %H:%M:%OS")
> z # prints without fractional seconds
[1] "2010-01-15 13:55:23 UTC"
> op <- options(digits.secs=3)
> z
[1] "2010-01-15 13:55:23.975 UTC"
> options(op) #reset options
You can also use strptime(time[1], "%OSn") where 0 <= n <= 6, without having to set digits.secs.
The documentation states "Which of these are supported is OS-dependent." so YMMV.
I have a separately created time series object with daily frequency:
my.timeseries= ts(data= 1:10, start= c(2014,1,1), frequency = 365.25)
How can I get back the dates as POSIXct vector ("2014-01-01 UTC" ...) from this time series object?
Here's one potential method. I'm not really sure if it should be done this way, but it seems to work.
With your existing time series, try
p <- paste(attr(my.timeseries, "tsp")[1], my.timeseries)
as.POSIXct(as.Date(p, "%Y %j"))
# [1] "2014-01-01 UTC" "2014-01-02 UTC" "2014-01-03 UTC"
# [4] "2014-01-04 UTC" "2014-01-05 UTC" "2014-01-06 UTC"
# [7] "2014-01-07 UTC" "2014-01-08 UTC" "2014-01-09 UTC"
# [10] "2014-01-10 UTC"
As noted by G. Grothendieck in the comments, here is a more general solution
p <- paste(start(my.timeseries), seq_along(my.timeseries))
as.Date(p, "%Y %j")
# [1] "2014-01-01" "2014-01-02" "2014-01-03" "2014-01-04"
# [5] "2014-01-05" "2014-01-06" "2014-01-07" "2014-01-08"
# [9] "2014-01-09" "2014-01-10"
as.Date might be better to avoid any time-zone issues.
I strongly advise you to use xts object instead of ts.
Here is a code replicating what you want :
library(xts)
my.index = seq(from = as.Date("2014-01-01"), by = "day", length.out = 10)
my.timeseries = xts(x = 1:10, order.by = my.index)
index(my.timeseries)
Let us know if that helps :)
Romain