Date shows up as number - r

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.

Related

Format Date via Parameter

I have a DateTime variable (default formatting), and I would like to format it to a format to I receive from a string parameter.
I normally do something similar to: {myDate:yyyy-MM-dd}, and it works properly.
Now I have a lot of possible date formats and need to format according to the chosen one.
I have tried the following but returned garbage (ae0aor0aa):
string testFormat = "yyyy. MM. dd.";
{myDate:testFormat }
I have also tried to convert the date to string and back to date with ParseExact, but gave me an invalid date exception. NB: the date in myDate is valid, as I have checked it with the debugger.
Can you kindly advise?
Thanks to apc, it was easily solved by myDate.ToString(testFormat)

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.

CAST() not changing to datetime format

I am using this SQL query to convert my _Submission_date column which of type nvarchar(max) to datetime format:
SELECT
CAST(PSCData._SUBMISSION_DATE AS DATETIME2)
FROM
PSCData
I have tried every possible way but still its giving this error:
Conversion failed when converting date and/or time from character string.
The data inside my _Submission_Date is in this form:
"2017-8-21 21:13:55.00000"
"2017-9-21 14:13:55.00000"
When I run this query it works fine:
SELECT CAST('2017-08-25' AS DATETIME);
but with this format :
SELECT CAST('2017-9-21 14:13:55.00000' AS DATETIME);
I get the same error mentioned above.
Any suggestions that how I can solve this?
Found the solution. The actual problem was the double quotes around the string which was causing the error it can be solved like this:
update PscData
SET _SUBMISSION_DATE = REPLACE(_SUBMISSION_DATE,'"', '')
select CAST(PSCData._SUBMISSION_DATE as DATETIME2)
FROM PSCData
i.e: first use the REPLACE method to remove double quotes around the string than you can use the cast method and can easily convert the "varchar" type data to "datetime" format without any problem. Plus u have to use "datetime2" in the cast method as "datetime" will not work with it.
Thanks for the help btw :)

character string is not in a standard unambiguous format

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.

Matching dates in sqldf

I have a data frame with stock data (date, symbol, high, low, open, close, volume). Using r and mysql and sqldf and rmysql I have a list of unique dates and unique stock symbols.
What I need now is to loop through the data and find the close on two specified dates. For instance:
stkData contains (date, symbol, high, low, open, close, volume)
dates contains unique dates
symbol contains unique symbols
I want to loop through the lists in a sqldf statement as such:
'select stkData$close from stkData where symbol = symbol[k] and date = dates[j]'
k and j would be looped numbers, but my problem is the symbol[k] and dates[j] parts.
sqldf won't read them properly (or I can't code properly). I've tried as.Date, as.character with no luck. I get the following error message:
Error in sqliteExecStatement(con, statement, bind.data) :
RS-DBI driver: (error in statement: near "[4,]": syntax error)
You're pretty far off in terms of syntax for sqldf, unfortunately. You can't use $ or [] notations in sqldf calls because those are both R syntax, not SQL syntax. It's an entirely separate language. What's happening is that sqldf is taking your data frame, importing it into SQLite3, executing the SQL query that you supply against the resulting table, and then importing the result set back into R as a data frame. No R functionality is available within the SQL.
It's not clear to me what you're trying to do, but if you want to run multiple queries in a loop, you probably want to construct the SQL query as a string using the R function paste(), so that when it gets to SQLite3 it'll just be static values where you currently have symbol[k] and dates[j].
So, you'll have something like the following, but wrapped in a loop for j and k:
sqldf(paste('select close from stkData where symbol = ', symbol[k],
' and date = ', dates[j]))
You might need to construct the select statement as a string with paste before it gets passed to your SQL caller. Something like:
combo_kj <- expand.grid(ksym=symbol[1:k], jdates=dates[1:j])
SQLcalls <- paste('select close from stkData where symbol = ',
combo_kj$ksym,
' and date = '
combo_kj$jdates,
sep="")
And then loop over SQLcalls with whatever code you are using.
Preface sqldf with fn$ as shown and then strings within backticks will be replaced by the result of running them in R and strings of the form $variable will be replaced by the contents of that variable (provided the variable name contains only word characters). Note that SQL requires that character constants be put in quotes so be sure to surround the backticks or $variable with quotes:
fn$sqldf("select close from stkData
where symbol = '`symbol[k]`' and
date = '`dates[j]`' ")
To use the $variable syntax try this:
mysymbol <- symbol[k]
mydate <- dates[j]
fn$sqldf("select close from stkData
where symbol = '$mysymbol' and
date = '$mydate' ")
Also see example 5 on the sqldf github page: https://github.com/ggrothendieck/sqldf

Resources