按日期子集 data.frame (Subset data.frame by date)


問題描述

按日期子集 data.frame (Subset data.frame by date)

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?

‑‑‑‑‑

參考解法

方法 1:

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

方法 2:

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

(by user1899793IRTFMcoip)

參考文件

  1. Subset data.frame by date (CC BY‑SA 3.0/4.0)

#R #Date #subset






相關問題

如何將均值、標準差等函數應用於整個矩陣 (How to apply mean, sd etc. function to a whole matrix)

Tạo các thùng của mỗi hàng trong bảng và vẽ hình thanh ngăn xếp trong R (Make bins of each table row and draw stack bar figure in R)

Reading not quite correct .csv file in R (Reading not quite correct .csv file in R)

包'treemap'中的線條粗細 (Thickness of lines in Package ‘treemap’)

是否需要帶有 awk 的預處理文件,或者可以直接在 R 中完成? (Is preprocessing file with awk needed or it can be done directly in R?)

rpivotTable 選擇元素下拉菜單 (rpivotTable select elements drop down menu)

優化性能 - Shiny 中的大文件輸入 (Optimizing Performance - Large File Input in Shiny)

數值取決於所應用的應用系列,R (Numeric values depending of apply family applied, R)

如何記錄全年的值? (How to note the values across year?)

R中的線性搜索 (Linear search in R)

在 dplyr/purrr 工作流程中動態連接多個數據集 (Dynamically join multiple datasets in a dplyr/purrr workflow)

如何將行值更改為列名 (R) (How change Row values to Column names (R))







留言討論