calcParticipation.Rd
Calculate participation rate by group. Participation rate is the number of customers divided by the population size.
calcParticipation(df, groupVars, pop)
df | A data frame |
---|---|
groupVars | A character vector of variable names to group by |
pop | A dataframe of population by county, year, gender, and age group. |
Other analysis functions: calcChurn
,
calcGenderProportion
,
calcRecruitment
,
countCustomers
, countItems
,
itemGroupCount
, sumRevenue
# Get list of valid counties (used to filter out data entry errors) validCounties <- countyMaps %>% dplyr::filter(region == "nebraska") %>% dplyr::distinct(county) %>% dplyr::pull(county) # Demo data: Participation rate for customers purchasing a fishing # license between 2010 and 2017 filterData( dataSource = "csv", activeFilters = list(itemType = "Fish", itemYear = c(2010, 2017), state = "NE") ) %>% calcParticipation(c("itemType", "county"), pop = statePop %>% filter(state == "NE") ) %>% dplyr::filter(county %in% validCounties)#> # A tibble: 91 x 5 #> itemType county customers pop participationRate #> <fct> <chr> <int> <dbl> <dbl> #> 1 Fish adams 104 31564 0.00329 #> 2 Fish antelope 31 6406 0.00484 #> 3 Fish arthur 1 421 0.00238 #> 4 Fish blaine 4 502 0.00797 #> 5 Fish boone 14 5357 0.00261 #> 6 Fish box butte 59 11200 0.00527 #> 7 Fish boyd 2 2049 0.000976 #> 8 Fish brown 11 3058 0.00360 #> 9 Fish buffalo 169 48834 0.00346 #> 10 Fish burt 18 6546 0.00275 #> # … with 81 more rowsif (FALSE) { # Database connection. Suggest using keyring package to avoid hardcoding # passwords myConn <- DBI::dbConnect(odbc::odbc(), dsn = "HuntFishApp", # Your datasource name uid = keyring::key_get("HuntFishAppUID"), # Your username pwd = keyring::key_get("HuntFishAppPWD") ) # Your password # Get list of valid counties (used to filter out data entry errors) validCounties <- countyMaps %>% dplyr::filter(region == "nebraska") %>% dplyr::distinct(county) %>% dplyr::pull(county) # SQL Backend: Participation rate for customers purchasing a fishing # license between 2010 and 2017 filterData( dataSource = "sql", conn = myConn, activeFilters = list(itemType = "Fish", itemYear = c(2010, 2017), state = "NE") ) %>% calcParticipation(c("itemType", "county"), pop = statePop %>% filter(state == "NE") ) %>% dplyr::filter(county %in% validCounties) }