Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. library(tidyverse)
  2.  
  3. # x is a grouped tibble, my_col is the column to peel
  4. my_function <- function(x, my_col){
  5.  
  6. my_col_enc <- enquo(my_col)
  7.  
  8. # Trying to grab the groups and then peel off the column
  9. x_grp <- x %>% group_vars()
  10. excluded <- x_grp[!is.element(x_grp, as.character(my_col_enc))]
  11.  
  12. # My calculations are two-tiered as described in the original description
  13. # simplifying for example
  14. x %>% group_by(excluded) %>% tally()
  15.  
  16. }
  17.  
  18. # This should be equivalent to mtcars %>% group_by(gear) %>% tally()
  19. mtcars %>% group_by(cyl, gear) %>% my_function(cyl)
  20.  
  21. library(tidyverse)
  22.  
  23. my_function <- function(x, my_col){
  24.  
  25. my_col_enc <- enquo(my_col)
  26.  
  27. # Trying to grab the groups and then peel off the column
  28. x_grp <- x %>% group_vars()
  29.  
  30. # here, make sure this is a symbol, else it'll group as character later (e.g. 'gear')
  31. excluded <- rlang::sym(x_grp[!is.element(x_grp, as.character(my_col_enc))])
  32.  
  33. # need to use !'s to deal with the symbol
  34. x %>% group_by(!!excluded) %>% tally()
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement