purrr
Split-Apply-Combinelibrary(tidyverse)
iris_tbl <- iris %>% as_tibble()
# calculate `Sepal.Length` mean by Species
species_v <- iris_tbl %>%
pull(Species) %>%
unique() %>%
as.character()
sepal_length_mean <- c()
for(i in seq_along(species_v)) {
species_tmp <- iris_tbl %>% filter(Species == species_v[i])
sepal_length_mean[i] <- species_tmp %>% summarise(mean(Sepal.Length)) %>% pull
}
sepal_length_mean
[1] 5.006 5.936 6.588
# normal function
calc_mean <- function(df) {
mean_val <- mean(df$`Sepal.Length`)
}
iris_split <- split(iris_tbl, iris_tbl$Species)
lapply(iris_split, calc_mean)
$setosa
[1] 5.006
$versicolor
[1] 5.936
$virginica
[1] 6.588
purrr
Split-Apply-Combine# dplyr verbs
iris_tbl %>%
group_by(Species) %>%
summarise(mean_sepal_length = mean(`Sepal.Length`))
# A tibble: 3 x 2
Species mean_sepal_length
<fct> <dbl>
1 setosa 5.01
2 versicolor 5.94
3 virginica 6.59
# group_by, nest, mutate, map pattern
iris_tbl %>%
group_by(Species) %>%
nest() %>%
mutate(mean_sepal_length = map_dbl(data, ~mean(.$`Sepal.Length`)))
# A tibble: 3 x 3
# Groups: Species [3]
Species data mean_sepal_length
<fct> <list<df[,4]>> <dbl>
1 setosa [50 × 4] 5.01
2 versicolor [50 × 4] 5.94
3 virginica [50 × 4] 6.59