nhl_rosters <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-01-09/nhl_rosters.csv')
## Rows: 54883 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): team_code, position_type, headshot, first_name, last_name, positi...
## dbl (7): season, player_id, sweater_number, height_in_inches, weight_in_po...
## date (1): birth_date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ncol_num <- nhl_rosters %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
ncol_num
## [1] 7
count_ncol_num <- function(.data){
ncol_num <- .data %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
return(ncol_num)
}
nhl_rosters %>% count_ncol_num
## [1] 7
count_ncol_type <- function(.data, type_data = "numeric"){
#if statement
if(type_data == "numeric") {
ncol_type <- .data %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
}else if(type_data == "character") {
ncol_type <- .data %>%
# Select a type of variables
select(where(is.character)) %>%
# Count columns
ncol() }
return(ncol_type) }
nhl_rosters %>% count_ncol_type(type_data = "character")
## [1] 10
nrow_num <- nhl_rosters %>%
filter(team_code == "ATL") %>%
nrow()
nrow_num
## [1] 410
count_nrow_num <- function(.data, team_code_input) {
nrow_num <- .data %>%
filter(team_code == team_code_input) %>%
nrow()
nrow_num
}
nhl_rosters %>% count_nrow_num(team_code_input = "ATL")
## [1] 410
nhl_rosters %>% count_nrow_num(team_code_input = "TOR")
## [1] 2944
Create your own.
nrow_num <- nhl_rosters %>%
filter(birth_state_province == "Alberta") %>%
nrow()
nrow_num
## [1] 4201
count_nrow_num <- function(.data, birth_prov_input) {
nrow_num <- .data %>%
filter(birth_state_province == birth_prov_input) %>%
nrow()
nrow_num
}
nhl_rosters %>% count_nrow_num(birth_prov_input = "Ontario")
## [1] 16378
nhl_rosters %>% count_nrow_num(birth_prov_input = "British Columbia")
## [1] 2724