Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subset data.frame by date

Tags:

date

r

subset

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?

like image 639
user1899793 Avatar asked Jan 23 '13 02:01

user1899793


2 Answers

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

like image 160
IRTFM Avatar answered Nov 20 '22 08:11

IRTFM


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"),]

like image 6
coip Avatar answered Nov 20 '22 07:11

coip