1

I'm attempting to write a function in R using dplyr that will allow me to take a data set, split it by a factor, and then run a series of other, more complicated, user defined functions on those subsets.

My problem is that I'm not sure how to specify the argument in the function call so that split() recognizes and correctly interprets the input.

Toy data and simplified functions below. I'd like to be able to run the function once on grp1 and once on grp2.

Many thanks for any thoughts/assistance!

library(tidyverse)

# Create toy data
res <- tibble(
  x = runif(n = 25, 1, 100),
  g1 = sample(x = 1:3, size = 25, replace = T),
  g2 = sample(x = 1:3, size = 25, replace = T)
)

# Apply function after splitting by grouping variable 1
res %>%
  split(.$g1) %>%
  map_df(~ mean(.$x))

# Write function to allow different grouping variables (tried to follow the programming advice re dplyr functions even though I know split is a base function)
new_func1 <- function(data_in, grp) {

  grp <- enquo(grp)

  data_in %>%
    split(!!grp) %>%
    map_df(~ mean(x))
}

# All result in errors
new_func1(data_in = res, grp = g1)
new_func1(data_in = res, grp = ".$g1")
new_func1(data_in = res, grp = quote(.$g1))

# Try using quote
new_func2 <- function(data_in, grp) {

  data_in %>%
    split(grp) %>%
    map_df(~ mean(x))
}

# All result in errors
new_func2(data_in = res, grp = g1)
new_func2(data_in = res, grp = ".$g1")
new_func2(data_in = res, grp = quote(.$g1))
2
  • 1
    If you're using dplyr why aren't you using group_by instead of split?
    – Dason
    Nov 18, 2017 at 22:35
  • Hi @Dason, my follow-up functions depend on multiple columns in each subset and some of them output multiple values. My understanding is that group_by is meant to work with summarize and summarize is for reducing results to summary type results only. Is that not correct? Nov 19, 2017 at 1:03

1 Answer 1

3

First, you cannot omit . in map_df(), map_df(~ mean(.$x)) is the correct one.

Second, split() is a base function, where you cannot use !!. !! is only effective if the function understands this notation. So, you can either

  1. unquote it inside such a function like pull().
  2. convert it to text.

For example:

new_func3 <- function(data_in, grp) {
  grp <- rlang::enquo(grp)

  data_in %>%
    split(pull(., !!grp)) %>%
    map_df(~ mean(.$x))
}

new_func4 <- function(data_in, grp) {
  grp <- rlang::enquo(grp)
  grp_chr <- rlang::quo_text(grp)

  data_in %>%
    split(.[[grp_chr]]) %>%
    map_df(~ mean(.$x))
}

Or, if you just want to pass grp as character, this is enough:

new_func5 <- function(data_in, grp_chr) {
  data_in %>%
    split(.[[grp_chr]]) %>%
    map_df(~ mean(.$x))
}
2
  • Brilliant @yutannihilation, many thanks. Passing grp as a character seemed the most straightforward approach and has worked excellently. I had never encountered ".[[ ]]" . Should be helpful in future. Nov 19, 2017 at 1:06
  • @JustinLeinaweaver Oh, [[ is a very basic operator in R and a thing you will find useful here and there. Good luck :) Nov 19, 2017 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.