Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204847

Complex Weighted Average in R by group by date

$
0
0

I have a data set of multiple products (3 in the example, 200 in real) and each product has a discount in percentage and belongs to a location on a specific day. The discount can change as well as the location and the value of the goods.

Date        Item    Discount    Value   Location
12/16/2019  AAA     4%          10000   BP1
12/17/2019  AAA     5%          11000   BP1
12/16/2019  AAA     6%          11500   BP2
12/16/2019  AAC     3%          10000   BP1
12/17/2019  AAC     4%          11000   BP1
12/16/2019  AAC     12%         13000   BP3
12/16/2019  BBC     12%         13500   BP3
12/17/2019  BBC     3%          11000   BP2
12/16/2019  BBC     1%          9000    BP1

I am looking to calculate the weighted average discount per location per date.

For example on 12/16/2019, W.BP1 (weighted average discount in BP1) is: 2.72% (Value of each item in PB1 divided by sum of the values, multiplied by the respective discount, then summed up).

The result would look like this:

Date        Location    Weighted.Discount
12/16/2019  BP1         2.72%
12/16/2019  …           …
12/16/2019  …           …

I can do it in excel for a few rows and locations, but need to find an R way of doing. Thank you in advance


Viewing all articles
Browse latest Browse all 204847

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>