I have trade data for year, months, commodity, and quantity. I want to create the total quantity, x_total, per commodity, per month, per year and have it appear as a new variable with the same number for each observation within that group.
For example:
What I have:
Year Month Commodity X_Quantity
2010 1 apples 10
2010 1 bananas 5
2010 2 apples 9
2010 2 bananas 4
What I want to see is:
Year Month Commodity X_Quantity X_total
2010 1 apples 10 15
2010 1 bananas 5 15
2010 2 apples 9 13
2010 2 bananas 4 13
my code so far looks like:
totals <- original.data [c("Year", "Month", "Commodity", "X_Quantity")] %>%
group_by(Year, Month, Commodity)%>%
summarise(X_total=sum(X_Quantity)) %>%
arrange(year, month, desc(X_total)) %>%
ungroup()
I have been using mutate to create previous variables.
I'd love to keep the X_Quantity variable to eventually create a X_share variable by dividing the quantity by the X_total for each Commodity.
I hope that makes sense, please forgive any posting errors I've committed (this is my first post).
Thanks so much in advance.