I am trying to create a data frame containing a set of calculations for different groups in the original data. A (silly) example is below:
library(tidyverse)
#> Warning: package 'dplyr' was built under R version 3.5.1
my_mean <- function(df, less_than) {
cyl <- max(df$cyl)
mean <- mean(df$mpg[df$mpg < less_than])
results <- list(cyl, mean)
names(results) <- c("cyl", paste("mean_lt", less_than, sep = "_"))
results
}
mtcars %>%
split(.$cyl) %>%
map_dfr(~ my_mean(df = .x, less_than = 30))
#> # A tibble: 3 x 2
#> cyl mean_lt_30
#> <dbl> <dbl>
#> 1 4 23.7
#> 2 6 19.7
#> 3 8 15.1
Created on 2018-10-19 by the reprex package (v0.2.1)
My question is whether it is possible to do the above for a set of levels of less_than. I could, of course, just use for over the levels and merge the resulting data frames on cyl, but where is the fun in that?! An example of what I am looking for using just two levels is at the end of the following:
library(tidyverse)
#> Warning: package 'dplyr' was built under R version 3.5.1
my_mean <- function(df, less_than) {
cyl <- max(df$cyl)
mean <- mean(df$mpg[df$mpg < less_than])
results <- list(cyl, mean)
names(results) <- c("cyl", paste("mean_lt", less_than, sep = "_"))
results
}
t1 <- mtcars %>%
split(.$cyl) %>%
map_dfr(~ my_mean(df = .x, less_than = 30))
t2 <- mtcars %>%
split(.$cyl) %>%
map_dfr(~ my_mean(df = .x, less_than = 40))
merge(t1, t2)
#> cyl mean_lt_30 mean_lt_40
#> 1 4 23.74286 26.66364
#> 2 6 19.74286 19.74286
#> 3 8 15.10000 15.10000
Created on 2018-10-19 by the reprex package (v0.2.1)
Finally, I should also mention that the calculations eventually will make their way into a bootstrapping set-up per group and level, but I am trying to do one step at the time.