Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. # function to compute summary stat
  2. compute_box_value <- function(data, vars, funs) {
  3. f = funs_(funs)
  4. result <- data %>%
  5. summarize_at(.cols = vars, .funs = f)
  6. }
  7.  
  8. # simple user defined function that gets count of rows with certain values of x
  9. equals <- function(x, test_value) {
  10. sum(x %in% test_value)
  11. }
  12.  
  13. x <- data.frame(value = sample(1:5, 10, TRUE))
  14.  
  15. vars <- c("value")
  16.  
  17. # this works
  18. print(compute_box_value(x, vars, "mean(., na.rm = TRUE)"))
  19.  
  20. # this works
  21. summarize_at(x, vars, .funs = "equals", test_value = 1)
  22.  
  23. # this doesn't work (error: couldn't find function equals)
  24. print(compute_box_value(x, vars, "equals(., test_value = 1)"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement