日期显示为数字

2024-03-03

我已经从 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='')

因此,在我尝试将 sql 粘贴在一起之前,R 会默默地将我的格式化日期转换为其整数表示形式。

如何获取日期的原始文本表示形式?我试过

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

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


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

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

由于某种原因,在循环内“日期”变量被视为整数,因此我必须明确将其转换为日期,然后对其进行格式化...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

日期显示为数字 的相关文章

随机推荐