【问题标题】:R Dataframe : Date range Operations - Subset those rows which falls under a certain date rangeR Dataframe:日期范围操作 - 子集落在某个日期范围内的那些行
【发布时间】:2014-12-16 06:00:00
【问题描述】:

如果您看到我的个人资料,我的所有问题都在数据框上,这是另一个问题!

我有一个特定的数据框,它是借记和贷记交易合并的结果

>head(allTxns)
   Cust_no    CreditDate   Credit     DebitDate   Debit
 1  12345     2014-10-01    200      2014-10-03    400
 2  12345     2014-10-01    200      2014-10-04    150
 3  12345     2014-10-01    200      2014-10-15    800     
 4  33344     2014-10-03    500      2014-10-04    50
 5  33344     2014-10-03    500      2014-10-05    504
 6  33344     2014-10-03    500      2014-10-06    332
 7  33344     2014-10-03    500      2014-10-08    56
 8  66554     2014-10-10    660      2014-10-04    150     
 9  66554     2014-10-10    660      2014-10-05    800
10  66554     2014-10-10    660      2014-10-11    400
11  66554     2014-10-10    660      2014-10-12    150
12  66554     2014-10-10    660      2014-10-13    800

我的目标是获取这些行,其中 DebitDate 位于 CreditDate 的 5 天之间,因此我尝试对数据进行子集化,并使用 : 运算符放置日期范围

FiveDays <- allTxns$CreditDate+5 #Results in a vector which has date + 5 days

allTxns <- cbind(allTxns[1:2],FiveDays,allTxns[4:6]) #Adding the vector as a column of dataframe

newDf <- allTxns[allTxns$DebitDate %in% allTxns$CreditDate:allTxns$FiveDays]

在上面的代码中,我得到了以下逻辑错误,其中只使用了第一个元素

Warning messages:
  1: In mer32$DepositDate:mer32$FiveDays2 :
      numerical expression has 3994 elements: only the first used
  2: In mer32$DepositDate:mer32$FiveDays2 :
      numerical expression has 3994 elements: only the first used

因此,我所需的输出仅限于第一个 Cust_no (12345),而不会应用于其他行。 如何确保范围条件适用于所有行?

输出不正确

 >head(newDf)
 row.names  Cust_no    CreditDate   Credit     DebitDate   Debit
     1      12345     2014-10-01    200      2014-10-03    400
     2      12345     2014-10-01    200      2014-10-04    150
     4      33344     2014-10-03    500      2014-10-04    50
     5      33344     2014-10-03    500      2014-10-05    504
     6      33344     2014-10-03    500      2014-10-06    332
     7      33344     2014-10-03    500      2014-10-08    56
     8      66554     2014-10-10    660      2014-10-04    150     
     9      66554     2014-10-10    660      2014-10-05    800
    10      66554     2014-10-10    660      2014-10-11    400
    11      66554     2014-10-10    660      2014-10-12    150
    12      66554     2014-10-10    660      2014-10-13    800

正确输出

 >head(newDf)
 row.names  Cust_no    CreditDate   Credit     DebitDate   Debit
     1      12345     2014-10-01    200      2014-10-03    400
     2      12345     2014-10-01    200      2014-10-04    150
     4      33344     2014-10-03    500      2014-10-04    50
     5      33344     2014-10-03    500      2014-10-05    504
     6      33344     2014-10-03    500      2014-10-06    332
     7      33344     2014-10-03    500      2014-10-08    56         
    10      66554     2014-10-10    660      2014-10-11    400
    11      66554     2014-10-10    660      2014-10-12    150
    12      66554     2014-10-10    660      2014-10-13    800

【问题讨论】:

    标签: r datetime dataframe subset date-range


    【解决方案1】:

    试试

     allTxns[with(allTxns , CreditDate < DebitDate & DebitDate <=FiveDays),]
     #    Cust_no CreditDate   FiveDays Credit  DebitDate Debit
     #1    12345 2014-10-01 2014-10-06    200 2014-10-03   400
     #2    12345 2014-10-01 2014-10-06    200 2014-10-04   150
     #4    33344 2014-10-03 2014-10-08    500 2014-10-04    50
     #5    33344 2014-10-03 2014-10-08    500 2014-10-05   504
     #6    33344 2014-10-03 2014-10-08    500 2014-10-06   332
     #7    33344 2014-10-03 2014-10-08    500 2014-10-08    56
     #10   66554 2014-10-10 2014-10-15    660 2014-10-11   400
     #11   66554 2014-10-10 2014-10-15    660 2014-10-12   150
     #12   66554 2014-10-10 2014-10-15    660 2014-10-13   800
    

    【讨论】:

      【解决方案2】:

      这个老问题已经有了一个公认的答案。但是,我注意到问题和答案可以简化,因为不需要创建额外的FiveDays 列:

      allTxns[with(allTxns, CreditDate <= DebitDate & DebitDate <= CreditDate + 5L), ]
      
         Cust_no CreditDate Credit  DebitDate Debit
      1    12345 2014-10-01    200 2014-10-03   400
      2    12345 2014-10-01    200 2014-10-04   150
      4    33344 2014-10-03    500 2014-10-04    50
      5    33344 2014-10-03    500 2014-10-05   504
      6    33344 2014-10-03    500 2014-10-06   332
      7    33344 2014-10-03    500 2014-10-08    56
      10   66554 2014-10-10    660 2014-10-11   400
      11   66554 2014-10-10    660 2014-10-12   150
      12   66554 2014-10-10    660 2014-10-13   800
      

      【讨论】:

        猜你喜欢
        • 2014-01-07
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多