data("mtcars")
mtcars <- as_tibble(mtcars)
Case of numeric variables
mtcars %>% map_dbl(.x = ., .f = ~mean(x = .x))
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
mtcars %>% map_dbl(.f = ~mean(x = .x))
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
mtcars %>% map_dbl(mean)
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
# Adding an argument
mtcars %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 0.1))
## mpg cyl disp hp drat wt
## 19.6961538 6.2307692 222.5230769 141.1923077 3.5792308 3.1526923
## qsec vs am gear carb
## 17.8276923 0.4230769 0.3846154 3.6153846 2.6538462
mtcars %>% map_dbl(mean, trim = 0.1)
## mpg cyl disp hp drat wt
## 19.6961538 6.2307692 222.5230769 141.1923077 3.5792308 3.1526923
## qsec vs am gear carb
## 17.8276923 0.4230769 0.3846154 3.6153846 2.6538462
mtcars %>% select(.data = .,mpg)
## # A tibble: 32 × 1
## mpg
## <dbl>
## 1 21
## 2 21
## 3 22.8
## 4 21.4
## 5 18.7
## 6 18.1
## 7 14.3
## 8 24.4
## 9 22.8
## 10 19.2
## # ℹ 22 more rows
mtcars %>% select(mpg)
## # A tibble: 32 × 1
## mpg
## <dbl>
## 1 21
## 2 21
## 3 22.8
## 4 21.4
## 5 18.7
## 6 18.1
## 7 14.3
## 8 24.4
## 9 22.8
## 10 19.2
## # ℹ 22 more rows
Create your own function
# Double values in columns
double_by_factor <- function(x, factor) {x * factor}
10 %>% double_by_factor(factor = 2)
## [1] 20
mtcars %>% map_dfr(.x = ., .f = ~double_by_factor(x = .x,factor = 10))
## # A tibble: 32 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 210 60 1600 1100 39 26.2 165. 0 10 40 40
## 2 210 60 1600 1100 39 28.8 170. 0 10 40 40
## 3 228 40 1080 930 38.5 23.2 186. 10 10 40 10
## 4 214 60 2580 1100 30.8 32.2 194. 10 0 30 10
## 5 187 80 3600 1750 31.5 34.4 170. 0 0 30 20
## 6 181 60 2250 1050 27.6 34.6 202. 10 0 30 10
## 7 143 80 3600 2450 32.1 35.7 158. 0 0 30 40
## 8 244 40 1467 620 36.9 31.9 200 10 0 40 20
## 9 228 40 1408 950 39.2 31.5 229 10 0 40 20
## 10 192 60 1676 1230 39.2 34.4 183 10 0 40 40
## # ℹ 22 more rows
mtcars %>% map_dfr(double_by_factor, factor = 10)
## # A tibble: 32 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 210 60 1600 1100 39 26.2 165. 0 10 40 40
## 2 210 60 1600 1100 39 28.8 170. 0 10 40 40
## 3 228 40 1080 930 38.5 23.2 186. 10 10 40 10
## 4 214 60 2580 1100 30.8 32.2 194. 10 0 30 10
## 5 187 80 3600 1750 31.5 34.4 170. 0 0 30 20
## 6 181 60 2250 1050 27.6 34.6 202. 10 0 30 10
## 7 143 80 3600 2450 32.1 35.7 158. 0 0 30 40
## 8 244 40 1467 620 36.9 31.9 200 10 0 40 20
## 9 228 40 1408 950 39.2 31.5 229 10 0 40 20
## 10 192 60 1676 1230 39.2 34.4 183 10 0 40 40
## # ℹ 22 more rows
When you have a grouping variable (factor)
mtcars %>% lm(formula = mpg ~ wt, data = .)
##
## Call:
## lm(formula = mpg ~ wt, data = .)
##
## Coefficients:
## (Intercept) wt
## 37.285 -5.344
mtcars %>% distinct(cyl)
## # A tibble: 3 × 1
## cyl
## <dbl>
## 1 6
## 2 4
## 3 8
reg_coeff_tbl <- mtcars %>%
# Split it into a list of data frames
split(.$cyl) %>%
# Repeat regression over each group
map(~lm(formula = mpg ~ wt, data = .x)) %>%
# Extract coefficients from regression results
map(broom::tidy, conf.int = TRUE) %>%
# Convert to tibble
bind_rows(.id = "cyl") %>%
# Filter for wt coefficients
filter(term == "wt")
reg_coeff_tbl %>%
mutate(estimate = -estimate,
conf.low = -conf.low,
conf.high = -conf.high) %>%
ggplot(aes(x = -estimate, y = cyl)) +
geom_point() +
geom_errorbar(aes(xmin = conf.low, xmax = conf.high))
Choose either one of the two cases above and apply it to your data
# Import data
palm <- read_excel("../00_data/palmtrees.xlsx")
## Warning: Coercing text to numeric in V1449 / R1449C22: '0.56675675700000006'
palm_numeric <- palm %>% select(where(is.numeric)) %>%
drop_na()
palm_numeric %>% map_dbl(mean)
## max_stem_height_m max_stem_dia_cm max_leaf_number
## 11.5378378 17.8921922 15.0030030
## max__blade__length_m max__rachis__length_m max__petiole_length_m
## 3.1230180 2.4252853 0.8855255
## average_fruit_length_cm min_fruit_length_cm max_fruit_length_cm
## 2.6228303 2.0991291 3.1308709
## average_fruit_width_cm min_fruit_width_cm max_fruit_width_cm
## 1.8314107 1.4670571 2.1560661
# Adding an argument
palm_numeric %>% map_dbl(mean, trim = 0.1)
## max_stem_height_m max_stem_dia_cm max_leaf_number
## 10.396629 15.575281 13.479401
## max__blade__length_m max__rachis__length_m max__petiole_length_m
## 2.618539 2.045393 0.730824
## average_fruit_length_cm min_fruit_length_cm max_fruit_length_cm
## 2.119260 1.721610 2.502247
## average_fruit_width_cm min_fruit_width_cm max_fruit_width_cm
## 1.508881 1.213521 1.757865
double_by_factor <- function(x, factor) {x * factor}
10 %>% double_by_factor(factor = 2)
## [1] 20
palm_numeric %>% map_dfr(double_by_factor, factor = 10)
## # A tibble: 333 × 12
## max_stem_height_m max_stem_dia_cm max_leaf_number max__blade__length_m
## <dbl> <dbl> <dbl> <dbl>
## 1 90 90 70 23.5
## 2 180 500 90 42
## 3 300 300 120 45
## 4 220 300 110 50
## 5 200 300 120 40
## 6 250 450 150 60
## 7 200 260 120 31.5
## 8 250 300 100 31.5
## 9 60 40 120 12.6
## 10 200 300 180 40
## # ℹ 323 more rows
## # ℹ 8 more variables: max__rachis__length_m <dbl>, max__petiole_length_m <dbl>,
## # average_fruit_length_cm <dbl>, min_fruit_length_cm <dbl>,
## # max_fruit_length_cm <dbl>, average_fruit_width_cm <dbl>,
## # min_fruit_width_cm <dbl>, max_fruit_width_cm <dbl>
fruit_mean <- function(palm) {
palm %>%
select(max_leaf_number) %>%
map_dbl(mean, na.rm = TRUE)
}
fruit_mean(palm)
## max_leaf_number
## 14.36524