基于日期比较R的子集数据集 [英] subset dataset based on date comparison R
本文介绍了基于日期比较R的子集数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据集如下所示
Col1 Col2 Col3 CutoffDate
12001是2008-08-15 2008 -08-10
12001是2008-08-22 2008-08-10
12001是2008-08-10 2008-08-10
12001是2008-08-04 2008-08 -10
我只想保留最后两行,因为它们小于或等于截止日期 2008-08-10 。
最终的数据集应该像这样
Col1 Col2 Col3 CutoffDate
12001是2008-08-10 2008-08-10
12001是2008-08-04 2008-08-10
我知道R中的子集功能,但不知道该怎么做,任何帮助都不胜感激。
解决方案
您可以使用常规比较
dat [dat $ Col3 <= dat $ CutoffDate,]
#Col1 Col2 Col3 CutoffDate
#3 12001是2008-08-10 2008-08-10
#4 12001是2008-08-04 2008-08-10
假设Col3和CuttoffDate是类Date
或者最好是,(dat,dat [Col3 <= CutoffDate,])
$ / code >
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.
解决方案
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, ])
这篇关于基于日期比较R的子集数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文