0

I have a dataset like this:

data.frame(car brand, car model, car sold, co2 emission)

And I want to get the total nb sold per car brand. So I did this:

x <- aggregate(carsold ~ carbrand, data=data.frame, FUN=sum)

Then, I want to get the average co2 emission per car brand.

y <- aggregate(co2emission ~ carbrand, data=data.frame, FUN=mean)

And finally I merge x and y

z <- merge(x, y, by="carbrand")

So I get this

data.frame(car brand, nb sold, average co2 emission)

Problem: in the dataset, nb of car sold may be different for each item. So sometimes, for one model I have 3 cars sold with a certain co2 emission, sometimes 1.

How can I aggregate the nb of car sold per car brand with the weighted average of co2 emission ?

Thanks

ScottJShea
  • 7,041
  • 11
  • 44
  • 67
Synleb
  • 143
  • 2
  • 2
  • 13
  • 1
    Check out `?weighted.mean`. – nrussell Nov 11 '14 at 22:18
  • 1
    This is not making any sense. Where is the weighting supposed to be applied? You wouldn't divide the car-model-CO2-means again by the number of cars sold. The division has already been done. – IRTFM Nov 11 '14 at 22:27
  • BondedDust: I know I'm wrong. But is there a way to compute weighted average of co2 emission regarding number of car sold for each car brand (car brand level, not car model) ? – Synleb Nov 11 '14 at 22:56
  • The values of (nb_sold * average_co2_emission) for each brand should give you what you want. – rnso Nov 12 '14 at 01:34

0 Answers0