Skip to content

Instantly share code, notes, and snippets.

@technocrat
Created March 25, 2021 22:38
Show Gist options
  • Save technocrat/8d5f08d971c20fb429f61114549931b0 to your computer and use it in GitHub Desktop.
Save technocrat/8d5f08d971c20fb429f61114549931b0 to your computer and use it in GitHub Desktop.
``` r
suppressPackageStartupMessages({
library(dplyr)
})
# create synthetic data
set.seed(42)
year_basket <- sample(2000:2020,100, replace = TRUE)
set.seed(137)
fee_basket <- sample(6000:9000,100)
synthetic <- tibble(Year = year_basket, Fee = fee_basket)
# group by Year and summarize stats
synthetic %>%
arrange(Year) %>%
group_by(Year) %>% summarize(
Count = n(),
Mean = mean(Fee),
SD = sd(Fee),
Median = median(Fee),
IQR = IQR(Fee)) -> output
get_output <- function(x,y,z) x[which(x$Year < y & x$IQR < z),]
get_output(output,2017,500)
#> # A tibble: 7 x 6
#> Year Count Mean SD Median IQR
#> <int> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 2001 5 7641. 855. 8112 486
#> 2 2002 5 7601. 997. 7714 75
#> 3 2005 4 7480. 308. 7548. 321.
#> 4 2006 3 6692. 455. 6735 453
#> 5 2009 5 7190. 451. 6930 322
#> 6 2011 2 6674. 29.0 6674. 20.5
#> 7 2015 3 7212 150. 7276 139
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment