為什麼這篇r資料分組cut鄉民發文收入到精華區:因為在r資料分組cut這個討論話題中,有許多相關的文章在討論,這篇最有參考價值!作者celestialgod (天)看板R_Language標題Re: [問題] 請問該怎麼寫讓函式...
※ 引述《locka (locka)》之銘言:
: [問題敘述]:
: 版上前輩大家晚安~
: 假設我的原始資料欄位有year,month,weekday,y等欄位
: 我想要對他重複做一樣的事情
: (根據不同的欄位grouping,計算每組的數量,組內y的平均然後畫圖)
: 因為差別只在於grouping的欄位不同,所以在想說可不可以用函數包起來
: ex:
: df_group_fn(df,"year","month") >>> 回傳以year,month欄位grouping後計算的結果
: df_group_fn(df,"month","weekday") >>> 回傳以month,weekday分組後計算的結果
: 也就是只要輸入該data frame跟要grouping的欄位
: 就可以直接回傳整理好的結果
: 原本想要用dplyr做,大概像下面這樣:
: df_group_fn <- function(df,col_1,col_2){
: df %>% group_by(col_1,col_2) %>% summarise(count=n(),avg=mean(y)) %>%
: ggplot(aes(mean,n)+geom_point()
: }
: 不過會卡在指定欄位參數因為是字串的關係, 在group_by那邊會有問題
: 所以試著改用data.table的寫法:
: df_group_fn <- function(df,col_1,col_2){
: df <- as.data.table(df)
: df[,`:=`(count=.N, avg=mean(y)),by=c(col_1,col_2)]
: ...
: }
: 可是data.table不會像dplyr一樣
: 產生只留下grouping跟summarise欄位的dataframe
: 他是在原始的data裡面新增欄位,這樣我就不知道怎麼畫圖了...
: 總結我的問題:
: 1. 希望有高手可以指點用dplyr跟data.table把function寫得更有彈性的方法
: 2. 如果我今天不想把grouping的欄位數量寫死,
: (例如我輸入"year"它就只根據year欄位分組,
: 輸入"year","month","weekday"就根據那三個欄位分組,該怎麼做呢?
: 3. 最後想問大家實務上會這麼做嗎? 很希望可以聽到版上大家分享!!
: 先謝謝各位版上先進了 m(_ _)m
: [關鍵字]:
:
: function, data.table, grouping
:
好讀版:http://pastebin.com/Yxres7jy
我會建議用wrapr去做這件事情
下面先把一般寫法列出給原PO參考
library(dplyr)
library(pipeR)
library(ggplot2)
library(data.table)
data("diamonds", package = "ggplot2")
# 一般寫法 (dplyr)
df_group_fn <- function(df, meanCol, col_1, col_2){
df %>>% group_by_(.dots = c(col_1, col_2)) %>>%
summarise_(.dots = c(n = "n()",
mean = paste0("mean(", meanCol, ")"))) %>>%
{ggplot(., aes(mean,n)) + geom_point()}
}
df_group_fn(diamonds, "price", "cut", "color")
# 一般寫法 (data.table)
dt_group_fn <- function(dt, meanCol, col_1, col_2){
dt[ , .(n = .N, mean = eval(parse(text = paste0("mean(", meanCol, ")")))),
by = c(col_1, col_2)] %>>%
{ggplot(., aes(mean,n)) + geom_point()}
}
dt_group_fn(data.table(diamonds), "price", "cut", "color")
# wrapr + dplyr
library(wrapr)
df_group_fn2 <- function(df, meanCol, col_1, col_2){
let(list(y = meanCol, c1 = col_1, c2 = col_2), {
df %>>% group_by(c1, c2) %>>% summarise(n = n(), mean = mean(y))
}) %>>% {ggplot(., aes(mean,n)) + geom_point()}
}
df_group_fn2(diamonds, "price", "cut", "color")
# wrapr + data.table
dt_group_fn2 <- function(dt, meanCol, col_1, col_2){
let(list(y = meanCol, c1 = col_1, c2 = col_2), {
dt[ , .(n = .N, mean = mean(y)), by = .(c1, c2)]
}) %>>% {ggplot(., aes(mean,n)) + geom_point()}
}
dt_group_fn2(data.table(diamonds), "price", "cut", "color")
# 進階,不把欄位給死的方法:
# dplyr
df_group_fn3 <- function(df, meanCol, groupByCols){
let(list(y = meanCol), {
df %>>% group_by_(.dots = groupByCols) %>>%
summarise(n = n(), mean = mean(y))
}) %>>%
{ggplot(., aes(mean,n)) + geom_point()}
}
df_group_fn3(diamonds, "price", c("cut", "color"))
# data.table
dt_group_fn3 <- function(dt, meanCol, groupByCols){
let(list(y = meanCol), {
dt[ , .(n = .N, mean = mean(y)), by = groupByCols]
}) %>>% {ggplot(., aes(mean,n)) + geom_point()}
}
dt_group_fn3(data.table(diamonds), "price", c("cut", "color"))
實務上,我自己是做比較接近data engineer的工作
基本上user就會有類似需要,這時候彈性的函數就顯得很重要
所以怎麼去利用eval, parse以及...就變得很重要 (沒看錯就是三個.)
除非全部都是處理data.frame,就可用dplyr透過lazyeval去做
不用wrapr,寫起來最漂亮的應該是下面這樣: (更正,應該是user用起來最爽XD)
# data.table + ... + substitute
dt_group_fn3 <- function(dt, meanCol, ...){
groupByCols <- as.character(as.list(substitute(list(...)))[-1L])
y <- substitute(meanCol)
dt[ , .(n = .N, mean = mean(y)), by = groupByCols] %>>%
{ggplot(., aes(mean,n)) + geom_point()}
}
dt_group_fn3(data.table(diamonds), price, cut, color)
--
R資料整理套件系列文:
magrittr #1LhSWhpH (R_Language) https://goo.gl/72l1m9
data.table #1LhW7Tvj (R_Language) https://goo.gl/PZa6Ue
dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/I5xX9b
tidyr #1Liqls1R (R_Language) https://goo.gl/i7yzAz
pipeR #1NXESRm5 (R_Language) https://goo.gl/zRUISx
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.235.41.96
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1489058786.A.745.html
這個我也不會教XD,去看advanced R吧,看你能學到多少了Orz
透過substitute轉成symbol / name,所以不需要quote~~