簡體   English   中英

計算從特定日期到今天的天數r

[英]Calculate number of days between specific date and today r

數據:

 DB <- data.frame(orderID  = c(1,2,3,4,5,6,7,8,9,10),     
   orderDate = c("1.1.14","16.3.14","11.5.14","21.6.14","29.7.14", 
        "2.8.14","21.9.14","4.10.14","30.11.14","2.1.15"),  
   itemID = c(2,3,2,5,12,4,2,3,1,5),  
   price = c(29.90, 39.90, 29.90, 19.90, 49.90, 9.90, 29.90, 39.90, 
              14.90, 19.90),
   customerID = c(1, 2, 3, 1, 1, 3, 2, 2, 1, 1),
   dateofbirth = c("12.1.67","14.10.82","6.8.87","12.1.67","12.1.67",
           "6.8.87","14.10.82","14.10.82","12.1.67","12.1.67")

預期的結果[希望我算正確的天數]:

1.daystilllastorder(here 18.02.2015) = c("47", "137", "200", "47",
"47", "200", "137", "137", "47", "47")
2.daysbetweenthelastorders = c("33", "13","83","33", "33", "83", "13", "13", "33", "33",)

嗨,大家好,

不幸的是,我有3個我無法單獨解決的新問題-因此,如果您再次偷看我,我將非常高興:)在數據集中,每個訂單都有自己的ID,每個注冊用戶都有其唯一的customerID。 每個客戶都可以訂購具有特定價格的商品(帶有商品ID)。 用戶將他/她的出生日期寫在數據庫中(如您在上面的:D所示)我想要1.計算從(每個客戶的)最后一次訂單到今天的天數。 2.計算實際(最新)和2.最新訂單之間的天數。3.總計訂單之間的天數

  1. 最近一個全年的訂單數量(不是從今天開始(2015年2月23日)-全年:此處1.1.2014-31.12.2014)當系統日期切換到2016年時,應該顯示2015年的訂單數量等等:希望這是可以理解的...

已經像這樣嘗試過了,但是不起作用:

setDT(DB)[, orderDate := as.Date(orderDate, format = "%Y-%m-%d")] 
DB[, daystilllastorder := sum(seq[max(orderDate),Sys.Date(),  by = customerID]
DB$orderDate <- as.factor(DB$orderDate)     

希望您能告訴我什么地方出了問題或告訴我解決此問題的另一種可能性。...

干杯和THX!

這是一個可能的解決方案(我不知道我是否正確理解了3點,但似乎您想要訂單之間的平均差?)

首先,我們將orderDate轉換為實際的日期類,然后一切簡單

setDT(DB)[, orderDate := as.Date(orderDate, "%d.%m.%y")]

DB[,  `:=`(
           daystillastord = Sys.Date() - max(orderDate),
           daysbetlastord = if(.N == 1L) "first order" else as.character(max(orderDate) - max(orderDate[orderDate != max(orderDate)])),
           meandiff = mean(diff(orderDate)),
           OrdsLastFullYear = sum(year(orderDate) == year(Sys.Date()) - 1)
           ),
   by = customerID][]

#     orderID  orderDate itemID price customerID dateofbirth daystillastord daysbetlastord meandiff OrdsLastFullYear
#  1:       1 2014-01-01      2  29.9          1     12.1.67        52 days             33     91.5                4
#  2:       2 2014-03-16      3  39.9          2    14.10.82       142 days             13    101.0                3
#  3:       3 2014-05-11      2  29.9          3      6.8.87       205 days             83     83.0                2
#  4:       4 2014-06-21      5  19.9          1     12.1.67        52 days             33     91.5                4
#  5:       5 2014-07-29     12  49.9          1     12.1.67        52 days             33     91.5                4
#  6:       6 2014-08-02      4   9.9          3      6.8.87       205 days             83     83.0                2
#  7:       7 2014-09-21      2  29.9          2    14.10.82       142 days             13    101.0                3
#  8:       8 2014-10-04      3  39.9          2    14.10.82       142 days             13    101.0                3
#  9:       9 2014-11-30      1  14.9          1     12.1.67        52 days             33     91.5                4
# 10:      10 2015-01-02      5  19.9          1     12.1.67        52 days             33     91.5                4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM