简体   繁体   中英

Basic statistics from aggregated data in R

I am just jumping into R now, so I may not been using the right terminology, please bear with me.

I have a table that shows the amount of cars that have a particular engine size:

Engine    Count
---------------
1800       20
1900       80
2000       40

So basically I have 140 cars (observations). When I call to summary(cars) I get individual stats of each column, but how can I get the median and mean of the engine size considering the proportions in count?

Thanks.

cars <- data.frame(Engine=c(1800, 1900, 2000),
                  Count=c(20, 80, 40))
cars
# Engine Count
# 1   1800    20
# 2   1900    80
# 3   2000    40
mean(rep(cars$Engine, cars$Count))
# [1] 1914.286
weighted.mean(x=cars$Engine, w=cars$Count)
# [1] 1914.286
median(rep(cars$Engine, cars$Count))
# [1] 1900

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM