title: map over specific groups with purrr in R
body: <p>New to purrr question:
  I am trying to vectorize my code by applying a function across groups in R using the map() package, and I just can't seem to get it right. Here's my example, working across copy-paste, for loop, and then map examples.</p>
  <pre><code>species &lt;- rep(seq(1:3), times = 10)
  bio_data &lt;- bind_cols(length = rnorm(n = 30, mean = 50, sd = 25), 
                        weight = rnorm(n = 30, mean = 100, sd = 35))
  
  dat &lt;- bind_cols(species = species, bio_data)
  
  # test --------------------------------------------------------------------
  
  # manually get mean by species:
  dat %&gt;% filter(species == 1) %&gt;% summarize(mean_wt = mean(weight))
  dat %&gt;% filter(species == 2) %&gt;% summarize(mean_wt = mean(weight))
  dat %&gt;% filter(species == 3) %&gt;% summarize(mean_wt = mean(weight))
  
  # create function and loop over function:
  get_mean &lt;- function(data, group){
    data %&gt;% 
      dplyr::filter(species == group) %&gt;% 
      summarise(mean_wt = mean(weight))
  }
  
  for(i in unique(species)){
    print(get_mean(dat, i))
  }
  
  # use purrr map() package
  results &lt;- map(dat, get_mean, group = unique(species))
  </code></pre>
  <p>This last line using map() with the get_mean() function above returns an error with the use of 'filter' and I'm not sure why?</p>
  <pre><code>Error in UseMethod(&quot;filter_&quot;) : 
  no applicable method for 'filter_' applied to an object of class &quot;c('integer', 'numeric')&quot; 
  </code></pre>
  <p>How can I get my code to produce the mean weights for each species?</p>