I have a data frame:
df <- data.frame(resource = c("gold", "gold", "gold", "silver", "silver", "gold", "silver", "bronze"), amount = c(500, 2000, 4, 8, 100, 2000, 3, 5), unit = c("g", "g", "kg", "ton", "kg", "g", "ton", "kg"), price = c(10, 10, 10000, 50000, 50, 10, 50000, 20))
I want to calculate the total amount of each resource in the most common unit.
My idea to approach this would be to:
- calculate the total value of each resource
- determine the most common unit of measurement and the corresponding price
- divide the total value by this price while maintaining a variable that indicates the measurement unit and ideally the price as well.
The result should look like
resource value  price unit amount
bronze   100    20     kg  100  
gold     85000  10     g   8500
silver   555000 50000  ton 11.1
If two measurement units are equally frequent it can be random, which one is used.
 
     
    