Data:
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")
Expected outcome [Hope I counted the number of days right]:
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",)
Hi guys,
unfortunately I have 3 new problems I´m not able to solve alone - so I would be very pleased if you peeps help me again :) In the data set every order got its own id and every registered user has his unique customerID. Every customer can order items (with ItemIDs), which got specific prices. User has his/her date of birth written in the data bank(as you can see above :D ) I want 1. Count the number of days from last order (of every customer) till today. 2. Count the number of days between the actual(newest) and the 2. newest order 3. Round the number of days between the orders in total
Tried it already like this, but it´s not working:
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)
Hope your able to show me what´s wrong or show me another posibility to solve the prob....
Cheers and THX!
Here's a possible solution (I don't know if I understood the 3 point correctly, but it seems like you want the average difference between orders?)
First we will convert orderDate
to an actuall date class, then everything is straight forward
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.