Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date shows up as number

Tags:

date

r

postgresql

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

like image 963
MortenSickel Avatar asked Jan 24 '13 08:01

MortenSickel


1 Answers

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...

like image 166
MortenSickel Avatar answered Sep 22 '22 14:09

MortenSickel