R子集按日期 [英] R subset by date

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

问题描述

我有一个名为 EPL2011_12 的数据集。我想通过按日期分组原始数据集来创建新的数据集。日期在名为日期的列中日期以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.

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

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