简体   繁体   English

按日期子集 data.frame

[英]Subset data.frame by date

I have a dataset called EPL2011_12 .我有一个名为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.日期位于名为Date的列中。​​日期采用 DD-MM-YY 格式。

I have tried我试过了

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

and

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

I guess that means R is treating like text instead of a number and that why it won't work?我想这意味着 R 正在处理文本而不是数字,这为什么不起作用?

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:错误消息和两条评论告诉您这是一个因素,但评论员显然正在等待并让消息陷入困境。 Dirk 建议您这样做:

 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 date functions assume the format is either "YYYY-MM-DD" or "YYYY/MM/DD". R 日期函数假定格式为“YYYY-MM-DD”或“YYYY/MM/DD”。 You do need to compare like classes: date to date, or character to character.您确实需要比较类似的类:日期到日期或字符到字符。 And if you were comparing character-to-character, then it's only going to be successful if the dates are in the YYYYMMDD format (with identical delimiters if any delimiters are used).如果您正在比较字符到字符,那么只有当日期采用 YYYYMMDD 格式时才会成功(如果使用任何分隔符,则使用相同的分隔符)。

The first thing you should do with date variables is confirm that R reads it as a Date.您应该对日期变量做的第一件事是确认 R 将其读取为日期。 To do this, for the variable (ie vector/column) called Date, in the data frame called EPL2011_12, input为此,对于名为 Date 的变量(即向量/列),在名为 EPL2011_12 的数据框中,输入

class(EPL2011_12$Date)

The output should read [1] "Date".输出应为 [1]“日期”。 If it doesn't, you should format it as a date by inputting如果没有,您应该通过输入将其格式化为日期

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

Note that the hyphens in the date format ("%d-%m-%y") above can also be slashes ("%d/%m/%y").请注意,上面日期格式(“%d-%m-%y”)中的连字符也可以是斜线(“%d/%m/%y”)。 Confirm that R sees it as a Date.确认 R 将其视为日期。 If it doesn't, try a different formatting command如果没有,请尝试不同的格式化命令

EPL2011_12$Date <- format(EPL2011_12$Date, format="%d/%m/%y")

Once you have it in Date format, you can use the subset command, or you can use brackets一旦你有了日期格式,你可以使用subset命令,或者你可以使用括号

WhateverYouWant <- EPL2011_12[EPL2011_12$Date > as.Date("2014-12-15"),]

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

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