I have one dataframe which looks like:
DF_1>
T_id D1 D2 Num type type_2 fig
xt-1 2017-05-01 2017-03-25 12:11:45 10 A X 25.20
xt-2 2017-05-01 2017-03-25 21:05:25 20 A Y 20.15
xt-3 2017-05-01 2017-03-25 08:10:55 25 B X 15.11
xt-4 2017-05-03 2017-03-25 07:19:35 30 B Y 22.56
xt-5 2017-05-03 2017-03-25 13:12:56 45 C Z 35.45
xt-6 2017-05-03 2017-03-25 18:14:44 20 D Z 27.21
xt-7 2017-04-06 2017-03-25 19:21:35 15 A Z 23.20
xt-8 2017-04-06 2017-03-25 21:11:15 40 C X 21.40
xt-9 2017-04-08 2017-02-25 22:25:04 20 A A 27.50
xt-10 2017-04-06 2017-02-25 16:04:08 30 A Y 32.20
xt-11 2017-04-05 2017-02-25 18:15:25 20 C Z 30.20
xt-12 2017-04-01 2017-01-25 19:22:25 50 A Z 33.15
xt-13 2017-04-02 2017-01-25 23:19:05 15 A A 30.12
xt-14 2017-03-03 2017-01-25 14:25:09 15 D Y 31.25
xt-15 2017-03-10 2017-01-25 23:25:36 40 A X 25.45
From above dataframe i want below mentioned two matrix:
1. Date (Last Three Date from `sys.date()`)
D1 count sum mean_num total_sum count_A sum_A count_other sum_other mean_fig mean_TAT
2017-05-03 3 95 31.66 6 0 0 3 95 28.40
2017-05-02 0 0 0 3 0 0 0 0 0.00
2017-05-01 3 55 18.33 3 2 30 1 25 20.15
- For calculation of
mean_TAT
: substractD2
-D1
and than take mean for that day based oncount
value for the same date. total_sum
would be cumulative from the first date of the month.count_A
andsum_A
based ontype
asA
for the particular day.count_other
andsum_other
for those wheretype
is other thanA
.
2.Based of Month (Last Three month as per the dataframe)
For based on the Month the format would be same only calculation would be month basis.
- There are 5 additional rows and 2 columns for each month where first three would be the top 3
type_2
based on count for the particular month. increase_%
would be calculated on previous month (i.e ifcount
for May-17 is 50 than Apr-17 100 than there would be -50% and same for the other 5 rows based on their previous monthcount
andsum
.- Fourth
A
would be constant for each month for the value wheretype_2
is "A". - Fifth
Other
would be the other than those 4type_2
as mentioned above. Total
would be as per the column forcount
andsum
there would be addition and formean
there would be mean.
It seems i couldn't explained properly, hope the matrix are understandable by dataframe.
Looking forward for some help.