Advertisement
celestialgod

wrapr + dplyr/data.table

Mar 9th, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.25 KB | None | 0 0
  1. library(dplyr)
  2. library(pipeR)
  3. library(ggplot2)
  4. library(data.table)
  5.  
  6. data("diamonds", package = "ggplot2")
  7.  
  8. # 一般寫法 (dplyr)
  9. df_group_fn <- function(df, meanCol, col_1, col_2){
  10.   df %>>% group_by_(.dots = c(col_1, col_2)) %>>%
  11.     summarise_(.dots = c(n = "n()", mean = paste0("mean(", meanCol, ")"))) %>>%
  12.     {ggplot(., aes(mean,n)) + geom_point()}
  13. }    
  14. df_group_fn(diamonds, "price", "cut", "color")
  15.  
  16. # 一般寫法 (data.table)
  17. dt_group_fn <- function(dt, meanCol, col_1, col_2){
  18.   dt[ , .(n = .N, mean = eval(parse(text = paste0("mean(", meanCol, ")")))), by = c(col_1, col_2)] %>>%
  19.   {ggplot(., aes(mean,n)) + geom_point()}
  20. }  
  21. dt_group_fn(data.table(diamonds), "price", "cut", "color")
  22.  
  23. # wrapr + dplyr
  24. library(wrapr)
  25. df_group_fn2 <- function(df, meanCol, col_1, col_2){
  26.   let(list(y = meanCol, c1 = col_1, c2 = col_2), {
  27.     df %>>% group_by(c1, c2) %>>%
  28.       summarise(n = n(), mean = mean(y))
  29.   }) %>>%
  30.   {ggplot(., aes(mean,n)) + geom_point()}
  31. }    
  32. df_group_fn2(diamonds, "price", "cut", "color")
  33.  
  34. # wrapr + data.table
  35. dt_group_fn2 <- function(dt, meanCol, col_1, col_2){
  36.   let(list(y = meanCol, c1 = col_1, c2 = col_2), {
  37.     dt[ , .(n = .N, mean = mean(y)), by = .(c1, c2)]
  38.   }) %>>% {ggplot(., aes(mean,n)) + geom_point()}
  39. }    
  40. dt_group_fn2(data.table(diamonds), "price", "cut", "color")
  41.  
  42. # 進階,不把欄位給死的方法:
  43. # dplyr
  44. df_group_fn3 <- function(df, meanCol, groupByCols){
  45.   let(list(y = meanCol), {
  46.     df %>>% group_by_(.dots = groupByCols) %>>%
  47.       summarise(n = n(), mean = mean(y))
  48.   }) %>>%
  49.   {ggplot(., aes(mean,n)) + geom_point()}
  50. }    
  51. df_group_fn3(diamonds, "price", c("cut", "color"))
  52.  
  53. # data.table
  54. dt_group_fn3 <- function(dt, meanCol, groupByCols){
  55.   let(list(y = meanCol), {
  56.     dt[ , .(n = .N, mean = mean(y)), by = groupByCols]
  57.   }) %>>% {ggplot(., aes(mean,n)) + geom_point()}
  58. }
  59. dt_group_fn3(data.table(diamonds), "price", c("cut", "color"))
  60.  
  61. # data.table + ... + substitute
  62. dt_group_fn3 <- function(dt, meanCol, ...){
  63.   groupByCols <- as.character(as.list(substitute(list(...)))[-1L])
  64.   y <- substitute(meanCol)
  65.   dt[ , .(n = .N, mean = mean(y)), by = groupByCols] %>>%
  66.     {ggplot(., aes(mean,n)) + geom_point()}
  67. }
  68. dt_group_fn3(data.table(diamonds), price, cut, color)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement