dplyr filter on Date

折月煮酒 提交于 2019-12-03 04:38:11
Pierre Lapointe

If Date is properly formatted as a date, your first try works:

p2p_dt_SKILL_A <-read.table(text="Patch,Date,Prod_DL
P1,9/4/2015,3.43
P11,9/11/2015,3.49
P12,9/18/2015,3.45
P13,12/6/2015,3.57
P14,12/13/2015,3.43
P15,12/20/2015,3.47
",sep=",",stringsAsFactors =FALSE, header=TRUE)

p2p_dt_SKILL_A$Date <-as.Date(p2p_dt_SKILL_A$Date,"%m/%d/%Y")

p2p_dt_SKILL_A%>%
                select(Patch,Date,Prod_DL)%>%
                filter(Date > "2015-09-04" & Date <"2015-09-18")
  Patch       Date Prod_DL
1 P11 2015-09-11    3.49

UPDATE

Still works if data is of type tbl_df.

p2p_dt_SKILL_A <-tbl_df(p2p_dt_SKILL_A)

p2p_dt_SKILL_A%>%
                select(Patch,Date,Prod_DL)%>%
                filter(Date > "2015-09-04" & Date <"2015-09-18")
Source: local data frame [1 x 3]

  Patch       Date Prod_DL
  (chr)     (date)   (dbl)
1 P11 2015-09-11    3.49

Another more verbose option would be to use the function between, a shortcut for x >= left & x <= right . We need to change the days to account for the = sign, and to use as.Date (explanation here).

p2p_dt_SKILL_A%>%
                select(Patch,Date,Prod_DL)%>%
                filter(between(Date, as.Date("2015-09-05"),as.Date("2015-09-17")))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!