character string is not in a standard unambiguous format - r

I have a dataframe (df3) with with some values.
One of these values is the daedlines.
The data of this value is something like the following:
deadline
1419397140
1418994978
1419984000
1418702400
They are days and I want to convert the to using this:
df3$deadline <- as.POSIXct(df3$deadline, origin="1970-01-01")
Generally it was worked for me with other dataframes from other files.
However with this it gives me back this error:
Error in as.POSIXlt.character(as.character(x), ...) :
character string is not in a standard unambiguous format
How can I fix it?

It might be that you have a character or factor, and it's expecting a numeric vector for conversion from unix time :
as.POSIXct(as.numeric(as.character(df3$deadline)),origin="1970-01-01")

As a suggestion for future debugging, you can check your parameter type by using
class(df3$deadline)
and making sure you are passing the correct type to as.POSIXlt().
From the help menu for asPOSIX*():
Character input is first converted to class '"POSIXlt"' by
'strptime': numeric input is first converted to '"POSIXct"'. Any
conversion that needs to go between the two date-time classes
requires a time zone: conversion from '"POSIXlt"' to '"POSIXct"'
will validate times in the selected time zone.

Related

Error in non-numeric argument to binary operator: opt parse and date formatting

I am working through a code on a sightings dataset and am trying to bin the dates into monthly (4 week) groups.
The command given to be used is:
breaks <- seq(min(min(survey.c$DATE), min(sightings.d$date))-opt$window_length*7,
max(max(survey.c$DATE), max(sightings.d$date))+opt$window_length*7,
by = paste(opt$window_length, 'week'))
This consistently gives back the error
Error in min(min(survey.c$DATE), min(sightings.d$date)) -
opt$window_length * : non-numeric argument to binary operator
Separately, part of the command (min(min(survey.c$DATE), min(sightings.d$date)) and (max(survey.c$DATE), max(sightings.d$date)) work, and so the issues comes with the last part opt$window_length.
The window_length is "2", however even if I change this last part to simply say "14" I run into the same issue. If I am totally honest, I don't completely understand exactly what opt$window_length is trying to do.
I have tried converting columns survey.c$DATE and sightings.d$date into date formats, and this reads back at true with no NAs. I can also convert these into characters, but the data reads back as NAs.
I think I need to change the final part of the command into a proper date format OR change the original date column into numeric form?

"Error in as.POSIXlt.numeric(value) : 'origin' must be supplied" When Trying To Add POSIXlt To Dataframe

I'm looking to write a script that can simply correct dates in a data frame (observations) that have two digit years to have four digits. I have all of the logic, but I get an error when I run this code:
observations[1,"Datetime_UTC"] <- observations[1,"Datetime_UTC"] + years(2000)
This line results in:
# Error in as.POSIXlt.numeric(value) : 'origin' must be supplied
How do I resolve this error?
Thank you!
If your dates are stored with some regular formatting, but with 2-digit years, just let lubridate functions handle it. They can handle most delimiters (even unusual ones).
library(lubridate)
ymd("20/11/9")
"2020-11-09"
dmy("31-1-90")
"1990-01-31"
mdy("3_07_00")
"2000-03-07"

Correct format for converting string to R date-time object

I've a system generated date and time format. It looks something like this, "2017-04-12-02.29.25.000000" . I want to convert this format into a standard one so that my system can read this and later on I can convert it into minutes. Someone please help to provide a code in R.
If you're unsure of the format, the guess_formats function in lubridate is pretty helpful:
w <- "2017-04-12-02.29.25.000000"
> lubridate::guess_formats(w, orders = 'YmdHMS')
YOmdHMS YmdHMS
"%Y-%Om-%d-%H.%M.%OS" "%Y-%m-%d-%H.%M.%OS"
orders is the format you want the function to investigate and it outputs the correct representation. If the second entry in the string is the day you can try YdmHMS.
The difference in the two formats in the output in the above example is based on formatting of the second entry (always with a leading zero or not). Trying the first format gives:
> as.POSIXct(w, format = "%Y-%Om-%d-%H.%M.%OS")
[1] "2017-04-12 02:29:25 EDT"
In the as.POSIXct call you may specify the timezone tz if required.

unable to retrieve POSIXct from numeric format after ifelse statement (R)

I have a table like so:
dtab<-data.table(Arr.Dep=c("A","D"),
time=c("2017-05-01 04:50:00","2017-05-01 04:55:00"))
dtab[,time:=parse_date_time(dtab$time, c("%y-%m-%d %H:%M%S"))]
Operations on the date and time column seem successful:
dtab[,time2:=time+2]
But if I try to run an ifelse statement, the POSIXct format goes back to numeric and I seem to be unable to bring it back to date and time.
dtab[,time3:=ifelse(dtab[,Arr.Dep]=="A",dtab[,time]+2,"hello")]
I saw the issue has already been raised:
R- date time variable loses format after ifelse
Unfortunately it's not of great help to me, as when I try to follow the example - adding 2 seconds rather than replacing with NA as in the OP -, I hit an error anyway.
Any help?
Use library(lubridate) and add time dtab$time2 <- dtab$time2 + seconds(2). With this method, the format does not change.

Date shows up as number

I have fetched a set of dates from postgresql, they look correct:
[1] "2007-07-13" "2007-07-14" "2007-07-22" "2007-07-23" "2007-07-24"
[6] "2007-07-25" "2007-08-13" "2007-08-14" "2007-08-15" "2007-08-16"
etc.
Then I want to run a loop on them to make new sql sentences to fetch some other data sets (yes, I know what I am doing, it would not have been possible to do all the processing in the database server)
So I tried
for(date in geilodates)
mapdate(date,geilo)
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR: invalid input syntax for type date: "13707"
LINE 1: ...id_date_location where not cowid is null and date='13707' or...
mapdate is a function I have written, the use of date within that is
sql=paste('select * from gps_coord where cowid=',cowid," and date='",date,"'",sep='')
So, what has happened is that R silently converted my formatted dates to their integer representations before i tried to paste the sql together.
How do I get the original textual representation of the date? I tried
for(date in geilodates){
d=as.Date(date,origin="1970-01-01")
mapdate(d,geilo)
}
Error in charToDate(x) :
character string is not in a standard unambiguous format
And I have not managed to find any other functions to create a datestring (or to "serve" the date as the string I get when listing the variable
Thanks to wush978 for pointing me in the right direction, In the end I had to do:
for(d in geilodates){
date=format(as.Date(d,origin="1970-01-01"))
mapdate(date,geilo)
}
For some reason, inside the loop the "date" variable was seen as an integer, so I explicitely had to convert it to a date and then format it...
try ?format.Date
x <- Sys.Date()
class(x)
class(format(x))
In R, the data of class Date is a numeric type.
An official way to represent Date as string is to call format.
I doubt that the format of date is defined in your case, so paste
does something unexpected.
Maybe you need to put format(x, "%Y-%m-%d") in your paste function instead of date to tell R how which format you want for Date.

Resources