charToDate(x)中的错误 [英] Error in charToDate(x)

查看:1710
本文介绍了charToDate(x)中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择一个仅包含当前日期信息的数据框架的子集。

I want to select a subset of a data.frame containing only information about the current date.

today = Sys.Date()
LasttDate = paste("'",today,"'",sep = "")
> LastDate
[1] "'2013-04-30'"

选择被执行通过以下包含日期的代码,它的工作原理

The selection is performed via the following code containing the date and it works

Lastdbdata = dbdata[dbdata$DateNav == '2013-04-30',]

如果我们不想将所有的时间都写在日期,但我们希望它是在运行代码时自动选择,但是我们可以编写

In case we do not want to write all the times the Date but we want it to be selected automatically when running the code I though we could write instead

    Lastdbdata = dbdata[dbdata$DateNav == LastDate,]
    Errore in charToDate(x) : 
    character string is not in a standard unambiguous format

但它不起作用并返回上面的错误。
这是解决这个错误的诀窍?

but it does not work and returns the error above. Which is the trick to solve this error?

推荐答案

你得到这个错误,因为 DateNav 已经在 Date 格式,而 Lastdate 不是。比较 DateNav 今天应该诀窍:

You are getting that error because DateNav is in Date format already, while Lastdate is not. Comparing DateNav to today should do the trick:

> Sys.Date()
[1] "2013-04-30"
> Sys.Date()==as.Date("2013-04-30")
[1] TRUE
> Sys.Date()==as.Date("'2013-04-30'")
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

事实上,即使 DateNav 不在日期格式,您可以/应该直接与今天进行比较。

In fact, even if DateNav were not in Date format, you could/should compare directly with today.

> Sys.Date()=="2013-04-30"
[1] TRUE

我不知道插入单引号的理由是什么。如果要将日期转换为字符,因为某些其他原因,总是有

I am not sure what the rationale is for inserting single quotes. If you want to convert the date to character for some other reason, there's always

> as(Sys.Date(),"character")
[1] "2013-04-30"

这篇关于charToDate(x)中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆