parse_date_time() converting DayofYear in date - r

Hi I'm using the lubridate package and
I want to convert a vector from 1:365 (day of year) in a date format:
e.g. 60 -> 2019-03-01 UTC.
For 1-99 it works fine, but for 100-365 I get a warning massage.
lubridate::parse_date_time(99, "j")
[1] "2019-04-09 UTC"
lubridate::parse_date_time(100:365, "j")
[1] NA ...
[365] NA
Warning message:
All formats failed to parse. No formats found.
Gets anyone the same warning massage or has a solution?

If you provide character input, it works well
lubridate::parse_date_time('100', "j")
# [1] "2019-04-10 UTC"
lubridate::parse_date_time(paste(100:365), "j")
# [1] "2019-04-10 UTC" "2019-04-11 UTC" "2019-04-12 UTC" "2019-04-13 UTC" "2019-04-14 UTC" "2019-04-15 UTC" "2019-04-16 UTC" "2019-04-17 UTC"
# ...
# [265] "2019-12-30 UTC" "2019-12-31 UTC

you can easily do it with specifying origin date using
as.Date(100:365, format = "%j", origin = "01-01-2019")

Related

Why does the lubridate::ymd_hms function add an NA observation when the "silent" argument is set TRUE?

Could any one explain why the "silent=T" argument triggers a warning and an NA observation, and tell me how to avoid this?
x <- c("2010-04-14-04-35-59", "20100401120000")
ymd_hms(x, silent=T)
[1] "2010-04-14 04:35:59 UTC" "2010-04-01 12:00:00 UTC" NA
Warning message:
1 failed to parse.
R version 3.4.0, lubridate version 1.6.0
Here, lubridate tries to evaluate "silent=T" as a date format, the argument for removing message being quiet.
lubridate::ymd_hms(x, quiet=TRUE)
[1] "2010-04-14 04:35:59 UTC" "2010-04-01 12:00:00 UTC"
This is because you can pass vector inside a lubridate function :
x <- c("2010-04-14-04-35-59", "20100401120000")
y <- c("2010-04-14-04-35-59", "20100401120000")
z <- c("2010-04-14-04-35-59", "20100401120000")
lubridate::ymd_hms(x, y, z)
[1] "2010-04-14 04:35:59 UTC" "2010-04-01 12:00:00 UTC"
[3] "2010-04-14 04:35:59 UTC" "2010-04-01 12:00:00 UTC"
[5] "2010-04-14 04:35:59 UTC" "2010-04-01 12:00:00 UTC"
So here, with silent=T, you're telling lubridate that silent=T is a vector to parse. Hence the NA.
I faced this issue for cases where the format is different. Please see that all the dates are following the same format. Using parse_date_time() can solve this problem.
parse_date_time(df$date, c("y/m/d","y/m/d HMS","m/d/y","m/d/y HM"))
Please be sure that the date format is contained in the list.

convert numeric variable into POSIXct

i have a variable that contains values about
" the beginning of time interval expressed as the number of millisecond elapsed from the Unix Epoch on January 1st, 1970 at UTC." (according to data source metadata)
This is the head:
x$timeInt
[1] 1.388068e+12 1.388075e+12 1.388096e+12 1.388051e+12 1.388051e+12 1.388072e+12
So i try to convert it as POSIXct
as.POSIXct(x$timeInt, origin = '01-01-1970',tz='UTC')
but i get this result
[1] "43987-03-01 05:20:00 UTC" "43987-05-23 13:20:00 UTC" "43988-01-28 13:20:00 UTC" "43986-08-25 17:20:00 UTC"
[5] "43986-08-25 17:20:00 UTC" "43987-04-25 18:40:00 UTC"
As you can see, the year is totally wrong. I tried using other formats in origin like "1970-01-01", but the result is the same.
I know thata data is taken in december 2013.
You have to take care, that this is in milliseconds, so:
x$timeInt <- x$timeInt/1000
And then one of the two approaches:
as.POSIXct(x$timeInt, origin = '1970-01-01',tz='UTC')
or
library(anytime)
anytime(x$timeInt)
#[1] "2013-12-26 15:26:40 CET" "2013-12-26 17:23:20 CET" "2013-12-26 23:13:20 CET" "2013-12-26 10:43:20 CET" "2013-12-26 10:43:20 CET"
#[6] "2013-12-26 16:33:20 CET"

Best way to deal with differing date data [duplicate]

I am trying to do some simple operation in R, after loading a table i encountered a date column which has many formats combined.
**Date**
1/28/14 6:43 PM
1/29/14 4:10 PM
1/30/14 12:09 PM
1/30/14 12:12 PM
02-03-14 19:49
02-03-14 20:03
02-05-14 14:33
I need to convert this to format like 28-01-2014 18:43 i.e. %d-%m-%y %h:%m
I tried this
tablename$Date <- as.Date(as.character(tablename$Date), "%d-%m-%y %h:%m")
but doing this its filling NA in the entire column. Please help me to get this right!
The lubridate package makes quick work of this:
library(lubridate)
d <- parse_date_time(dates, names(guess_formats(dates, c("mdy HM", "mdy IMp"))))
d
## [1] "2014-01-28 18:43:00 UTC" "2014-01-29 16:10:00 UTC"
## [3] "2014-01-30 12:09:00 UTC" "2014-01-30 12:12:00 UTC"
## [5] "2014-02-03 19:49:00 UTC" "2014-02-03 20:03:00 UTC"
## [7] "2014-02-05 14:33:00 UTC"
# put in desired format
format(d, "%m-%d-%Y %H:%M:%S")
## [1] "01-28-2014 18:43:00" "01-29-2014 16:10:00" "01-30-2014 12:09:00"
## [4] "01-30-2014 12:12:00" "02-03-2014 19:49:00" "02-03-2014 20:03:00"
## [7] "02-05-2014 14:33:00"
You'll need to adjust the vector in guess_formats if you come across other format variations.

Get date of timeseries object

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

Changing dates in different time zones by adding to POSIXlt

I am running into an error when I try to localize times for "date" (a variable of class=POSIXlt) in my dataset. Example code is as follows:
# All dates are coded by survey software in EST(not local time)
date <- c("2011-07-26 07:23", "2011-07-29 07:34", "2011-07-29 07:40")
region <-c("USA-EST", "UK", "Singapore")
#Change the times based on time-zone differences
start_time<-strptime(date,"%Y-%m-%d %h:%m")
localtime=as.POSIXlt(start_time)
localtime<-ifelse(region=="UK",start_time+6,start_time)
localtime<-ifelse(region=="Singapore",start_time+12,start_time)
#Then, I need to extract the hour and weekday
weekday<-weekdays(localtime)
hour<-factor(localtime)
There must be something wrong with my "ifelse" statement, because I get the error: number of items to replace is not a multiple of replacement length. Please help!
How about using R's native time code? The trick is that you can't have more than one time-zone in a POSIX vector, so use a list instead:
region <- c("EST","Europe/London","Asia/Singapore")
(localtime <- lapply(seq(date),function(x) as.POSIXlt(date[x],tz=region[x])))
[[1]]
[1] "2011-07-26 07:23:00 EST"
[[2]]
[1] "2011-07-29 07:34:00 Europe/London"
[[3]]
[1] "2011-07-29 07:40:00 Asia/Singapore"
And to convert to a vector in a single timezone:
Reduce("c",localtime)
[1] "2011-07-26 13:23:00 BST" "2011-07-29 07:34:00 BST"
[3] "2011-07-29 00:40:00 BST"
Note that my system timezone is BST, but if yours is EST it will convert to that.
You can use the timezone handling built in in POSIXct:
> start_time <- as.POSIXct(date,"%Y-%m-%d %H:%M", tz = "America/New_York")
> start_time
[1] "2011-07-26 07:23:00 EDT" "2011-07-29 07:34:00 EDT" "2011-07-29 07:40:00 EDT"
> format(start_time, tz="Europe/London", usetz=TRUE)
[1] "2011-07-26 12:23:00 BST" "2011-07-29 12:34:00 BST" "2011-07-29 12:40:00 BST"
> format(start_time, tz="Asia/Singapore", usetz=TRUE)
[1] "2011-07-26 19:23:00 SGT" "2011-07-29 19:34:00 SGT" "2011-07-29 19:40:00 SGT"

Resources