As a follow up to this question, what is the best way if you would like to calculate the percentage not as a share inside of the group, but rather as a share of the column total.
d <- read.table(text="Fuel Year Region Count
Gasoline 2013 GE 169600
Diesel 2013 GE 46790
Hybrid 2013 GE 2268
Electric 2013 GE 85
Other 2013 GE 532
Gasoline 2013 VS 149232
Diesel 2013 VS 50591
Hybrid 2013 VS 1028
Electric 2013 VS 268
Other 2013 VS 261", header = TRUE)
d <- data.table(d)
I would then like to calculate the share of fuels irrespective of the regions. So in a first step I would like to have this:
d[, .(Car.Total = sum(Count)), by = "Fuel"]
Is there a better way to calculate the percentage than this:
d[, .(Car.Total = sum(Count)
, Car.Share = sum(Count)/sum(d[,Count])), by = "Fuel"]
This seems pretty inefficient, but works. Is there any more efficient way using only data.table
methods.