I´d like to calculate a column based on a logic between tables. Let me explain, given a data.frame with this structure:
Transaction - Start - End - Quantity - Area
(Number) - (Date) - (Date) - (Number) - (Number)
Start
and End
determine how long has a Transaction
been in an Area
. I want to calculate the stock in each Area
for each day.
What is stock? The sum of Quantity that follows this logic:
Start <= day AND End >= day
OR
Start <= day AND End == NULL
What is day? Each day of the calendar. (Thus the logic between tables.)
How can the stock per day per area be calculated in R? Example data:
calendar <- as.data.frame(seq.Date(as.Date("2019-01-01"), as.Date("2019-01-10"), 1))
colnames(calendar) <- c("Date")
> head(calendar)
Date
1 2019-01-01
2 2019-01-02
3 2019-01-03
4 2019-01-04
5 2019-01-05
6 2019-01-06
Transaction <- c(299784, 299785, 301913, 302840, 305722, 285874, 285875, 312587, 326842, 328521)
Start <- as.Date(c("2019-01-01", "2019-01-01", "2019-01-02", "2019-01-02", "2019-01-03", "2019-01-01", "2019-01-01", "2019-01-02", "2019-01-02", "2019-01-03"))
End <- as.Date(c("2019-01-05", "2019-01-04", "2019-01-06", "2019-01-03", "NULL", "2019-01-05", "2019-01-04", "2019-01-06", "2019-01-03", "NULL"))
Quantity <- c(1,1,1,1,1,1,1,1,1,1)
Area <- c(7065, 7065, 7065, 7065, 7065, 6098, 6098, 6098, 6098, 6098)
df <- data.frame(Transaction, Start, End, Quantity, Area)
> df
Transaction Start End Quantity Area
1 299784 2019-01-01 2019-01-05 1 7065
2 299785 2019-01-01 2019-01-04 1 7065
3 301913 2019-01-02 2019-01-06 1 7065
4 302840 2019-01-02 2019-01-03 1 7065
5 305722 2019-01-03 <NA> 1 7065
6 285874 2019-01-01 2019-01-05 1 6098
7 285875 2019-01-01 2019-01-04 1 6098
8 312587 2019-01-02 2019-01-06 1 6098
9 326842 2019-01-02 2019-01-03 1 6098
10 328521 2019-01-03 <NA> 1 6098
The stock each day would be:
Date Area Stock
1 2019-01-01 7065 2
2 2019-01-02 7065 4
3 2019-01-03 7065 5
4 2019-01-04 7065 4
5 2019-01-05 7065 3
6 2019-01-06 7065 2
7 2019-01-07 7065 1
8 2019-01-08 7065 1
9 2019-01-09 7065 1
10 2019-01-10 7065 1
11 2019-01-01 6098 2
12 2019-01-02 6098 4
13 2019-01-03 6098 5
14 2019-01-04 6098 4
15 2019-01-05 6098 3
16 2019-01-06 6098 2
17 2019-01-07 6098 1
18 2019-01-08 6098 1
19 2019-01-09 6098 1
20 2019-01-10 6098 1
or:
Date 7065 6098
1 2019-01-01 2 2
2 2019-01-02 4 4
3 2019-01-03 5 5
4 2019-01-04 4 4
5 2019-01-05 3 3
6 2019-01-06 1 1
7 2019-01-07 1 1
8 2019-01-08 1 1
9 2019-01-09 1 1
10 2019-01-10 1 1