繁体   English   中英

日期显示为数字

[英]Date shows up as number

我从PostgreSQL获取了一组日期,它们看起来正确:

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

然后,我想在它们上运行一个循环以制作新的sql语句以获取其他一些数据集(是的,我知道我在做什么,不可能在数据库服务器中进行所有处理)

所以我尝试了

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是我编写的函数,其中的日期用法是

sql=paste('select * from gps_coord where cowid=',cowid," and date='",date,"'",sep='')

因此,发生的事情是R在我尝试将sql粘贴到一起之前,无声地将我的格式化日期转换为其整数表示形式。

如何获得日期的原始文字表示? 我试过了

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

而且我还没有找到其他创建日期字符串的函数(或将日期“保留”为列出变量时得到的字符串)

尝试?format.Date

x <- Sys.Date()
class(x)
class(format(x))

在R中, Date类的Date是数字类型。 Date表示为字符串的一种正式方法是调用format

我怀疑date格式是否适合您的情况,因此paste会发生意外情况。

也许您需要在paste函数中放入format(x, "%Y-%m-%d")而不是date以告诉R您希望日期使用哪种格式。

感谢wush978为我指出了正确的方向,最后我不得不做:

for(d in geilodates){
 date=format(as.Date(d,origin="1970-01-01"))
 mapdate(date,geilo)
}

出于某种原因,在循环中,“ date”变量被视为整数,因此我明确地不得不将其转换为日期,然后对其进行格式化...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM