Subset data.frame by date

前端 未结 2 505
南笙
南笙 2020-11-29 05:08

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

相关标签:
2条回答
  • 2020-11-29 05:44

    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.

    0 讨论(0)
  • 2020-11-29 05:49

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

    0 讨论(0)
提交回复
热议问题