0

所以我有一个看起来像这样的数据框:

    ID Initialdate  Finaldate
  1405  2003-12-03 2010-12-07
  7044  2004-12-08 2011-10-13
  7219  2008-05-16 2009-06-04
 18618  2004-06-17 2012-02-13
 19900  2005-06-01 2008-06-11
 20138  2010-01-20 2010-01-20
 29067  2003-04-30 2004-09-10
 33546  2003-11-25 2008-10-10
 37321  2003-06-07 2006-03-20
 43028  2004-09-23 2008-07-25
 43591  2005-04-06 2005-11-15
 46749  2005-02-28 2005-05-16
 48846  2005-08-02 2005-08-02
114353  2002-05-17 2006-10-26
128180  2004-06-17 2010-06-21
128648  2003-05-07 2009-07-23
133337  2004-05-26 2012-07-26
149181  2002-10-19 2008-07-27
214079  2003-09-26 2007-05-20
215060  2006-04-17 2011-08-17
229816  2007-04-25 2011-09-24
238123  2007-11-26 2012-01-31
253776  2006-03-02 2012-04-19
258660  2010-03-25 2012-04-09
265356  2002-04-22 2002-04-22

我使用以下代码制作了第四列,其中包含最终日期和初始日期之间的差异,并将其清理为:

df$Duration<-(difftime(df$Finaldate, df$Initialdate, units = "days"))
df$Duration<-as.numeric(df$Duration, units = "days")

我得到以下输出,这让我很高兴:

    ID Initialdate  Finaldate   Duration
  1405  2003-12-03 2010-12-07 2561.00000
  7044  2004-12-08 2011-10-13 2499.95833
  7219  2008-05-16 2009-06-04  384.00000
 18618  2004-06-17 2012-02-13 2797.04167
 19900  2005-06-01 2008-06-11 1106.00000
 20138  2010-01-20 2010-01-20    0.00000
 29067  2003-04-30 2004-09-10  499.00000
 33546  2003-11-25 2008-10-10 1780.95833
 37321  2003-06-07 2006-03-20 1017.04167
 43028  2004-09-23 2008-07-25 1401.00000
 43591  2005-04-06 2005-11-15  223.04167
 46749  2005-02-28 2005-05-16   76.95833
 48846  2005-08-02 2005-08-02    0.00000
114353  2002-05-17 2006-10-26 1623.00000
128180  2004-06-17 2010-06-21 2195.00000
128648  2003-05-07 2009-07-23 2269.00000
133337  2004-05-26 2012-07-26 2983.00000
149181  2002-10-19 2008-07-27 2108.00000
214079  2003-09-26 2007-05-20 1332.00000
215060  2006-04-17 2011-08-17 1948.00000
229816  2007-04-25 2011-09-24 1613.00000
238123  2007-11-26 2012-01-31 1527.00000
253776  2006-03-02 2012-04-19 2239.95833
258660  2010-03-25 2012-04-09  746.00000
265356  2002-04-22 2002-04-22    0.00000

我从这里开始的计划是矢量化持续时间数据,特别是那些少于 180 天的数据,然后使用该新数据帧使用如下代码从初始数据帧中删除这些 ID# df_final<-df[!(df$ID %in% unqualified$ID),]:但是,当我这样做时:

unqualified<-(df[df$Duration <= '179.000',])

我得到这个输出,这绝对是不正确的:

    ID Initialdate  Finaldate Duration
 19900  2005-06-01 2008-06-11 1106.000
 20138  2010-01-20 2010-01-20    0.000
 33546  2003-11-25 2008-10-10 1780.958
 37321  2003-06-07 2006-03-20 1017.042
 43028  2004-09-23 2008-07-25 1401.000
 48846  2005-08-02 2005-08-02    0.000
114353  2002-05-17 2006-10-26 1623.000
214079  2003-09-26 2007-05-20 1332.000
229816  2007-04-25 2011-09-24 1613.000
238123  2007-11-26 2012-01-31 1527.000
265356  2002-04-22 2002-04-22    0.000

我想可能是因为持续时间的数字有问题,但是当我运行时它们被列为数字sapply(unqualified, class)并且sapply(unqualified, mode). 我还应该提到,在我的编码之前,我确实使用 strptime 转换了日期以确保它们是正确的。我四处寻找,试图找出问题所在,但一切都在 Millhouse 出现......任何帮助将不胜感激

4

1 回答 1

1

像这样怎么样:

unqualified<-(df[df$Duration < 180,])

即你的号码作为一个数字,而不是一个字符串。

于 2013-09-13T18:21:00.033 回答