My dataframe has 2 categorical variables, and one is of a lower hierarchy than the other. I want to sum the numerical value of all rows within the sub-category using dplyr.
Thank you in advance for all those who can help me with!
This is the dataframe I start with:
transportation <- data.frame(
Country = c("A", "A", "A", "B", "B", "B"),
Mode = c("Car", "Train", "Plane", "Car", "Train", "Plane"),
Energy = c(10000, 9000, 20000, 200000, 160000, 450000)
)
And this is the dataframe I want to end up with:
country_sum <- data.frame(
Country = c("A", "A", "A", "B", "B", "B"),
Mode = c("Car", "Train", "Plane", "Car", "Train", "Plane"),
Energy = c(10000, 9000, 20000, 200000, 160000, 450000),
country_sum = c(39000, 39000, 39000, 810000, 810000, 810000)
)