This function is a small wrapper around percentify, where the upper bound is a fixed value.

percentify_max(data, var, q = numeric(), upper = 1)

Arguments

data

A data.frame or tibble,

var

Variable to do grouping by as string or symbol.

q

Numerical values for lower bound of ranges. Must be between 0 and 1.

upper

Numerical values for upper bound of ranges. Must be between 0 and 1. Length of lower and upper must be equal.

Value

percentile grouped tibble

Details

There is a ggplot2::autoplot() to visualize the the percentile ranges.

See also

Examples

library(dplyr) library(broom) percent_mtcars <- percentify_max(mtcars, mpg, c(0, 0.25, 0.5, 0.75)) percent_mtcars
#> # A tibble: 32 x 11 #> # Groups: .percentile_mpg [4] #> mpg cyl disp hp drat wt qsec vs am gear carb #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 #> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 #> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 #> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 #> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 #> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 #> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 #> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 #> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 #> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 #> # … with 22 more rows
summarize(percent_mtcars, mean_hp = mean(hp), mean_wt = mean(wt), n_obs = n() )
#> # A tibble: 4 x 4 #> .percentile_mpg mean_hp mean_wt n_obs #> <chr> <dbl> <dbl> <int> #> 1 0%-100% 147. 3.22 32 #> 2 25%-100% 120. 2.86 24 #> 3 50%-100% 101. 2.58 17 #> 4 75%-100% 78.1 2.21 9
percent_mtcars %>% group_modify(~tidy(lm(disp ~ wt + cyl, data = .x)))
#> # A tibble: 11 x 6 #> # Groups: .percentile_mpg [4] #> .percentile_mpg term estimate std.error statistic p.value #> <chr> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 0%-100% (Intercept) -190. 27.2 -7.00 0.000000107 #> 2 0%-100% wt 59.5 12.0 4.96 0.0000281 #> 3 0%-100% cyl 37.1 6.57 5.65 0.00000423 #> 4 25%-100% (Intercept) -145. 33.5 -4.34 0.000288 #> 5 25%-100% wt 25.5 18.3 1.40 0.177 #> 6 25%-100% cyl 45.9 7.97 5.76 0.0000102 #> 7 50%-100% (Intercept) -163. 40.1 -4.05 0.00120 #> 8 50%-100% wt 46.7 19.2 2.43 0.0289 #> 9 50%-100% cyl 38.5 10.3 3.74 0.00220 #> 10 75%-100% (Intercept) 12.1 21.7 0.557 0.595 #> 11 75%-100% wt 40.5 9.51 4.26 0.00373
library(ggplot2) autoplot(percent_mtcars)