在汇总时使用均值时避免自动向上舍入

时间:2018-03-16 02:35:54

标签: r ggplot2 plyr summarize roundup

如何使用汇总避免平均值(价格)的自动舍入? 我想得到像Results1一样的结果。

> data(diamonds, package="ggplot2")    
> head(diamonds)
    # A tibble: 6 x 10
      carat cut       color clarity depth table price     x     y     z
      <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
    1 0.230 Ideal     E     SI2      61.5   55.   326  3.95  3.98  2.43
    2 0.210 Premium   E     SI1      59.8   61.   326  3.89  3.84  2.31
    3 0.230 Good      E     VS1      56.9   65.   327  4.05  4.07  2.31
    4 0.290 Premium   I     VS2      62.4   58.   334  4.20  4.23  2.63
    5 0.310 Good      J     SI2      63.3   58.   335  4.34  4.35  2.75
    6 0.240 Very Good J     VVS2     62.8   57.   336  3.94  3.96  2.48

结果1:

> aggregate(price~cut, diamonds, mean, na.rm=TRUE)
        cut    price
1      Fair 4358.758
2      Good 3928.864
3 Very Good 3981.760
4   Premium 4584.258
5     Ideal 3457.542

结果2:

> (diamonds %>% group_by(cut) %>% summarize(AvgPrice = mean(price)))
# A tibble: 5 x 2
  cut       AvgPrice
  <ord>        <dbl>
1 Fair         4359.
2 Good         3929.
3 Very Good    3982.
4 Premium      4584.
5 Ideal        3458.

0 个答案:

没有答案
相关问题