powerrangers <- read_csv("../00_data/PowerRangers.csv")
## Rows: 922 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): season, title, air.date, description
## dbl (3): episode, imdb.rating, total.votes
##
## ℹ 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 <- powerrangers %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
ncol_num
## [1] 3
count_ncol_numeric <- function(.powerrangers) {
# body
ncol_num <- .powerrangers %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
# return the new variable
return(ncol_num)
}
powerrangers %>% count_ncol_numeric()
## [1] 3
powerrangers %>% .[1:7] %>% count_ncol_numeric
## [1] 3
count_ncol_type <- function(.powerrangers, type_data = "numeric") {
# Determine the type of variable to count
if (type_data == "numeric") {
ncol_type <- .powerrangers %>%
select(where(is.numeric)) %>%
ncol()
} else if (type_data == "character") {
ncol_type <- .powerrangers %>%
select(where(is.character)) %>%
ncol()
} else {
stop("Invalid type_data specified. Use 'numeric' or 'character'.")
}
# Return the count
return(ncol_type)
}
powerrangers %>% count_ncol_type()
## [1] 3
powerrangers %>% count_ncol_type(type_data = "character")
## [1] 4
powerrangers %>%
.[1:10, 1:5] %>% # Subset the first 10 rows and first 5 columns
count_ncol_type(type_data = "character")
## [1] 3
nrow_num <- powerrangers %>%
# filter rows that meet a condition
filter(season == "UA") %>%
# Count rows
nrow()
nrow_num
## [1] 0
count_num_flights_by_carrier <- function(.powerrangers, season_name) {
# Check if the season column exists
if (!"season" %in% colnames(.powerrangers)) {
stop("The column 'season' does not exist in the dataset.")
}
# Count rows matching the season
nrow_num <- .powerrangers %>%
filter(season == season_name) %>%
nrow()
# Return the count
return(nrow_num)
}
powerrangers %>% filter(season == "Ninja Steel") %>% count_num_flights_by_carrier(season_name = "Ninja Steel")
## [1] 22
Create your own.
powerrangers <- data.frame(
season = c("Ninja Steel", "Dino Charge", "Dino Charge", "Ninja Steel", "Beast Morphers"),
carrier = c("UA", "Delta", "UA", "Southwest", "Delta"),
episodes = c(20, 22, 22, 20, 19)
)
# Preview the dataset
print(powerrangers)
## season carrier episodes
## 1 Ninja Steel UA 20
## 2 Dino Charge Delta 22
## 3 Dino Charge UA 22
## 4 Ninja Steel Southwest 20
## 5 Beast Morphers Delta 19
# Count rows where season is "Ninja Steel"
ninja_steel_count <- powerrangers %>%
filter(season == "Ninja Steel") %>%
nrow()
# Print the result
print(ninja_steel_count)
## [1] 2
Use the filter() function to select rows that meet a condition. Refer to Chapter 5.2 Filter rows with filter()