0

how can I avoid automatic Round-up in mean(price) using summarize? I'd like to get the result like 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

Results1 :

> 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

Results2:

> (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.
user2357112
  • 260,549
  • 28
  • 431
  • 505
JennyY
  • 1
  • 1
  • 1
    They are the same. You can try to write them to files. – Tung Mar 16 '18 at 02:42
  • @Tung oh thanks.! btw, if i wan to check same results on console as well, is there any option? it would be very annoying if I have to check with files not output console... – JennyY Mar 16 '18 at 04:23
  • Use `View()` within RStudio – Tung Mar 16 '18 at 05:00
  • Add something like `%>% mutate(AvgPrice.view = format(AvgPrice, nsmall = 3))` to your code if you want to see the result in the output console. – Z.Lin Mar 16 '18 at 05:05
  • @Z.Lin !! Thanks! it works. i needed to change the format with 'nsmall') :) – JennyY Mar 16 '18 at 05:08
  • @JennyY Glad that it helps. :) But do note that `format()` is for pretty viewing / printing, and results in a character string. If you need to use the values for calculation, stick with the original numeric values. – Z.Lin Mar 16 '18 at 05:11
  • @Z.Lin sure:) it could be just perfect if I don't need any additional functions like mutate in order to see original data not like round-upped one using summarize with mean function. (or better to use another function for exact value :p ) – JennyY Mar 16 '18 at 05:40
  • You can use `%>% as.data.frame()` to convert the tibble to a data.frame to see more decimal places – Richard Telford Mar 16 '18 at 09:41

0 Answers0