子集data.frame按日期 [英] Subset data.frame by date

查看:67
本文介绍了子集data.frame按日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 EPL2011_12 的数据集。我想通过按日期设置原始数据集来创建新的数据集。日期在名为 Date 的列中。日期采用DD-MM-YY格式。

I have a dataset called EPL2011_12. I would like to make new a dataset by subsetting the original by date. The dates are in the column named Date The dates are in DD-MM-YY format.

我尝试过

EPL2011_12FirstHalf <- subset(EPL2011_12, Date > 13-01-12)

EPL2011_12FirstHalf <- subset(EPL2011_12, Date > "13-01-12")

,但每次都会收到此错误消息。

but get this error message each time.


Warning message:
In Ops.factor(Date, 13- 1 - 12) : > not meaningful for factors


来说没有意义,我想这意味着R在对待文本而不是数字,为什么它不起作用?

I guess that means R is treating like text instead of a number and that why it won't work?

推荐答案

好吧,它显然不是数字,因为它带有破折号它。错误消息和两个注释告诉您这是一个因素,但注释程序显然正在等待并让消息沉没。Dirk建议您执行以下操作:

Well, it's clearly not a number since it has dashes in it. The error message and the two comments tell you that it is a factor but the commentators are apparently waiting and letting the message sink in. Dirk is suggesting that you do this:

 EPL2011_12$Date2 <- as.Date( as.character(EPL2011_12$Date), "%d-%m-%y")

之后,您可以执行以下操作:

After that you can do this:

 EPL2011_12FirstHalf <- subset(EPL2011_12, Date2 > as.Date("2012-01-13") )

R日期函数假定格式为 YYYY-MM-DD或 YYYY / MM / DD。您确实需要比较类似的类:日期到日期或字符到字符。

R date functions assume the format is either "YYYY-MM-DD" or "YYYY/MM/DD". You do need to compare like classes: date to date, or character to character.

这篇关于子集data.frame按日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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