32

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.

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?

Jaap
  • 81,064
  • 34
  • 182
  • 193
user1899793
  • 421
  • 1
  • 4
  • 4
  • 2
    It means your Date column was read in as a factor (presumably when using `read.table` or some similar function). You'll need to convert it using e.g. `as.Date`. – joran Jan 23 '13 at 02:50
  • Don't forget `as.character()`, eg use `as.Date(as.character(X), "%d-%m-%y")`. – Dirk Eddelbuettel Jan 23 '13 at 02:59
  • @DirkEddelbuettel Isn't there an `as.Date.factor` method, or am I misunderstanding what that does...? – joran Jan 23 '13 at 03:07
  • Interesting. That must be new-ish. I had been bitten so often by `as.Date(factorvar)` in the past with `factorvar` becoming its levels... – Dirk Eddelbuettel Jan 23 '13 at 03:14

2 Answers2

53

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 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. 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).

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • If there are other date types like 2013/01/01 13:34:59 you may refer to https://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html for more format detail. – huangli Jun 19 '15 at 13:59
6

The first thing you should do with date variables is confirm that R reads it as a Date. To do this, for the variable (i.e. vector/column) called Date, in the data frame called EPL2011_12, input

class(EPL2011_12$Date)

The output should read [1] "Date". 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"). Confirm that R sees it as a Date. 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

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

coip
  • 1,312
  • 16
  • 30