games <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2020/2020-02-04/games.csv')
## Rows: 5324 Columns: 19
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): week, home_team, away_team, winner, tie, day, date, home_team_nam...
## dbl (7): year, pts_win, pts_loss, yds_win, turnovers_win, yds_loss, turnov...
## time (1): time
##
## ℹ 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 <- games %>%
# 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)
}
games %>% count_ncol_num
## [1] 7
games %>% .[1:10, 1:5] %>% count_ncol_num()
## [1] 1
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) }
games %>% count_ncol_type(type_data = "character")
## [1] 11
games %>% .[1:10, 1:5] %>% count_ncol_type(type_data = "character")
## [1] 4
nrow_num <- games %>%
# filter rows that meet a condition
filter(day == "Thu") %>%
# Count rows
nrow()
nrow_num
## [1] 214
count_num_games_by_day <- function(.data, day_name) {
nrow_num <- .data %>%
# filter rows that meet a condition
filter(day == day_name) %>%
# Count rows
nrow()
nrow_num
}
games %>% count_num_games_by_day(day_name = "Mon")
## [1] 339
games %>% count_num_games_by_day(day_name = "Thu")
## [1] 214
games %>% count_num_games_by_day("Sun")
## [1] 4588
Create your own.
nrow_num <- games %>%
# filter rows that meet a condition
filter(week == "1") %>%
# Count rows
nrow()
nrow_num
## [1] 317
count_nrow_num_by_week <- function(.data, week_num){
nrow_num <- .data %>%
# filter rows that meet a condition
filter(week == week_num) %>%
# Count rows
nrow()
nrow_num
}
games %>% count_nrow_num_by_week(1)
## [1] 317
games %>% count_nrow_num_by_week(5)
## [1] 282
games %>% count_nrow_num_by_week(10)
## [1] 285