자주 쓰는 group_by summarise 를 function으로 정의하기




group_by에 사용할 변수의 개수에 상관없이 사용할 수 있도록 


  • Use ... in the function definition so our function can accept any number of arguments.

  • Use quos() to capture all the ... as a list of formulas.

  • Use !!! instead of !! to splice the arguments into group_by().


my_summarise <- function(df, ...) {
  group_var <- quos(...)

  df %>%
    group_by(!!! group_var) %>%
    summarise(a = mean(a))
}

출처 : https://dplyr.tidyverse.org/articles/programming.html


'R study' 카테고리의 다른 글

R 사용 환경 구축하기  (0) 2018.07.08

+ Recent posts