Skip to contents

Return keys nearest to a given statistics or summary.

Usage

# S3 method for data.frame
keys_near(.data, key, var, top_n = 1, funs = l_five_num, ...)

Arguments

.data

data.frame

key

key, which identifies unique observations.

var

variable to summarise

top_n

top number of closest observations to return - default is 1, which will also return ties.

funs

named list of functions to summarise by. Default is a given list of the five number summary, l_five_num.

...

extra arguments to pass to mutate_at when performing the summary as given by funs.

Examples

heights %>%
  key_slope(height_cm ~ year) %>%
  keys_near(key = country,
            var = .slope_year)
#> # A tibble: 6 × 5
#>   country    .slope_year stat  stat_value stat_diff
#>   <chr>            <dbl> <fct>      <dbl>     <dbl>
#> 1 Austria         0.0695 q_75      0.0690  0.000515
#> 2 Burundi         0.321  max       0.321   0       
#> 3 Eritrea        -0.102  min      -0.102   0       
#> 4 Mali            0.0401 med       0.0403  0.000120
#> 5 Spain           0.0404 med       0.0403  0.000120
#> 6 Tajikistan      0.0199 q_25      0.0205  0.000632
# Specify your own list of summaries
l_ranges <- list(min = b_min,
                 range_diff = b_range_diff,
                 max = b_max,
                 iqr = b_iqr)

heights %>%
  key_slope(formula = height_cm ~ year) %>%
  keys_near(key = country,
              var = .slope_year,
              funs = l_ranges)
#> # A tibble: 4 × 5
#>   country     .slope_year stat       stat_value stat_diff
#>   <chr>             <dbl> <fct>           <dbl>     <dbl>
#> 1 Burundi          0.321  range_diff     0.424    0.102  
#> 2 Burundi          0.321  max            0.321    0      
#> 3 Eritrea         -0.102  min           -0.102    0      
#> 4 Switzerland      0.0496 iqr            0.0485   0.00116