subset dataset based on date comparison R

拈花ヽ惹草 提交于 2021-02-05 06:52:10

问题


I have a dataset as shown below

    Col1      Col2       Col3        CutoffDate
    12001     Yes        2008-08-15  2008-08-10
    12001     Yes        2008-08-22  2008-08-10
    12001     Yes        2008-08-10  2008-08-10
    12001     Yes        2008-08-04  2008-08-10

I am only interested in retaining the last two rows because they are less than or equal to the Cutoff Date 2008-08-10.

The final dataset should look like this

    Col1      Col2       Col3        CutoffDate
    12001     Yes        2008-08-10  2008-08-10
    12001     Yes        2008-08-04  2008-08-10

I know the subset function in R but not sure how to do this , any help is much appreciated.


回答1:


You can just use regular comparison

dat[dat$Col3 <= dat$CutoffDate, ]
#    Col1 Col2       Col3 CutoffDate
# 3 12001  Yes 2008-08-10 2008-08-10
# 4 12001  Yes 2008-08-04 2008-08-10

Assuming Col3 and CuttoffDate are class "Date"

or maybe preferably,

with(dat, dat[Col3 <= CutoffDate, ])



回答2:


You can use subset():

df <- data.frame(Col1=c(12001,12001,12001,12001),Col2=c('Yes','Yes','Yes','Yes'),Col3=as.Date(c('2008-08-15','2008-08-22','2008-08-10','2008-08-04')),CutoffDate=as.Date(c('2008-08-10','2008-08-10','2008-08-10','2008-08-10')));
subset(df,Col3<=CutoffDate);
##    Col1 Col2       Col3 CutoffDate
## 3 12001  Yes 2008-08-10 2008-08-10
## 4 12001  Yes 2008-08-04 2008-08-10



回答3:


And if you are using dplyr:

library(dplyr)
df <- data.frame(Col1 = c(12001, 12001, 12001, 12001),
                 Col2 = c("Yes", "Yes", "Yes", "Yes"),
                 Col3 = as.Date(c("2008-08-15", "2008-08-22", "2008-08-10", "2008-08-04")),
                 CutoffDate = as.Date(c("2008-08-10", "2008-08-10", "2008-08-10", "2008-08-10")))

df %>% filter(Col3 <= CutoffDate)


来源:https://stackoverflow.com/questions/31665165/subset-dataset-based-on-date-comparison-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!