I have the dataframe below
df = data.frame(season = rep(seq(1,4),2)
,product = c(rep('A', 4), rep('B', 4))
,revenue = 1:8
)
I am looking to calculate each season's revenue as a % of total (inside each product's partition) such that the end table has the following column created
df$pc = c(0.1, 0.2, 0.3, 0.4, 0.19, 0.23, 0.27, 0.31)
I am aware this is achievable with packages such as dplyr
as discussed here:
Summarizing by subgroup percentage in R
However, the challenge is to achieve this with base R functions or a combination of base R and user defined functions.
Any help would be much appreciated.