SHARE
TWEET

Untitled

a guest Jun 15th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library(dplyr)
  2. g_mtcars <- group_by(mtcars, cyl, gear)
  3. summarise(g_mtcars, mean (hp))
  4.  
  5. # Source: local data frame [8 x 3]
  6. # Groups: cyl [?]
  7. #
  8. #     cyl  gear `mean(hp)`
  9. #   <dbl> <dbl>      <dbl>
  10. # 1     4     3    97.0000
  11. # 2     4     4    76.0000
  12. # 3     4     5   102.0000
  13. # 4     6     3   107.5000
  14. # 5     6     4   116.5000
  15. # 6     6     5   175.0000
  16. # 7     8     3   194.1667
  17. # 8     8     5   299.5000
  18.      
  19. library(dplyr)
  20. mtcars %>%
  21.     group_by(cyl, gear) %>%
  22.     summarise_all("mean")
  23.  
  24. # Source: local data frame [8 x 11]
  25. # Groups: cyl [?]
  26. #
  27. #     cyl  gear    mpg     disp       hp     drat       wt    qsec    vs    am     carb
  28. #   <dbl> <dbl>  <dbl>    <dbl>    <dbl>    <dbl>    <dbl>   <dbl> <dbl> <dbl>    <dbl>
  29. # 1     4     3 21.500 120.1000  97.0000 3.700000 2.465000 20.0100   1.0  0.00 1.000000
  30. # 2     4     4 26.925 102.6250  76.0000 4.110000 2.378125 19.6125   1.0  0.75 1.500000
  31. # 3     4     5 28.200 107.7000 102.0000 4.100000 1.826500 16.8000   0.5  1.00 2.000000
  32. # 4     6     3 19.750 241.5000 107.5000 2.920000 3.337500 19.8300   1.0  0.00 1.000000
  33. # 5     6     4 19.750 163.8000 116.5000 3.910000 3.093750 17.6700   0.5  0.50 4.000000
  34. # 6     6     5 19.700 145.0000 175.0000 3.620000 2.770000 15.5000   0.0  1.00 6.000000
  35. # 7     8     3 15.050 357.6167 194.1667 3.120833 4.104083 17.1425   0.0  0.00 3.083333
  36. # 8     8     5 15.400 326.0000 299.5000 3.880000 3.370000 14.5500   0.0  1.00 6.000000
  37.      
  38. library(data.table)
  39.  setDT(mtcars)[ , lapply(.SD, mean) , by=c("cyl", "gear")]
  40.      
  41. aggregate(. ~ cyl + gear, data = mtcars, FUN = mean)
  42. #   cyl gear    mpg     disp       hp     drat       wt    qsec  vs   am     carb
  43. # 1   4    3 21.500 120.1000  97.0000 3.700000 2.465000 20.0100 1.0 0.00 1.000000
  44. # 2   6    3 19.750 241.5000 107.5000 2.920000 3.337500 19.8300 1.0 0.00 1.000000
  45. # 3   8    3 15.050 357.6167 194.1667 3.120833 4.104083 17.1425 0.0 0.00 3.083333
  46. # 4   4    4 26.925 102.6250  76.0000 4.110000 2.378125 19.6125 1.0 0.75 1.500000
  47. # 5   6    4 19.750 163.8000 116.5000 3.910000 3.093750 17.6700 0.5 0.50 4.000000
  48. # 6   4    5 28.200 107.7000 102.0000 4.100000 1.826500 16.8000 0.5 1.00 2.000000
  49. # 7   6    5 19.700 145.0000 175.0000 3.620000 2.770000 15.5000 0.0 1.00 6.000000
  50. # 8   8    5 15.400 326.0000 299.5000 3.880000 3.370000 14.5500 0.0 1.00 6.000000
  51.      
  52. library(dplyr)
  53.  
  54. mtcars %>%
  55.   group_by(cyl, gear) %>%
  56.   summarize(mean_hp = mean(hp), mean_wt = mean(wt))
  57.  
  58. # Source: local data frame [8 x 4]
  59. # Groups: cyl [?]
  60.  
  61. #     cyl  gear  mean_hp  mean_wt
  62. #   <dbl> <dbl>    <dbl>    <dbl>
  63. # 1     4     3  97.0000 2.465000
  64. # 2     4     4  76.0000 2.378125
  65. # 3     4     5 102.0000 1.826500
  66. # 4     6     3 107.5000 3.337500
  67. # 5     6     4 116.5000 3.093750
  68. # 6     6     5 175.0000 2.770000
  69. # 7     8     3 194.1667 4.104083
  70. # 8     8     5 299.5000 3.370000
  71.      
  72. library(plyr)
  73. ddply(mtcars,c('cyl','gear'), summarize,mean_hp=mean(hp))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top