fires <- read_xlsx("myData.xlsx")
data(fires)
## Warning in data(fires): data set 'fires' not found
fires %>% skimr::skim()
Name | Piped data |
Number of rows | 83 |
Number of columns | 7 |
_______________________ | |
Column type frequency: | |
character | 2 |
numeric | 5 |
________________________ | |
Group variables | None |
Variable type: character
skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
---|---|---|---|---|---|---|---|
YEAR | 0 | 1 | 4 | 4 | 0 | 83 | 0 |
SEVERITY | 0 | 1 | 4 | 8 | 0 | 3 | 0 |
Variable type: numeric
skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
---|---|---|---|---|---|---|---|---|---|---|
NUMBER_FIRES | 0 | 1 | 4970.61 | 2166.51 | 1447.00 | 3011.50 | 5170.00 | 6735.00 | 1.003800e+04 | ▇▆▇▆▂ |
ACRES_BURNED | 0 | 1 | 191990.29 | 147346.79 | 23154.00 | 98818.50 | 140818.00 | 227953.00 | 7.566960e+05 | ▇▃▁▁▁ |
DAMAGE_COSTS | 0 | 1 | 91267464.19 | 361878036.32 | 151584.00 | 1424995.00 | 9958751.00 | 42492940.50 | 3.061837e+09 | ▇▁▁▁▁ |
Damage_Cost_Per_Acre | 0 | 1 | 409.46 | 1190.52 | 1.30 | 8.30 | 109.70 | 370.14 | 1.028681e+04 | ▇▁▁▁▁ |
Average_Acres_Burned_Per_Fire | 0 | 1 | 52.51 | 57.52 | 3.71 | 16.24 | 34.69 | 65.16 | 3.190000e+02 | ▇▂▁▁▁ |
ncol_num <- fires %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
ncol_num
## [1] 5
count_ncol_numeric <- function(.data) {
#body
ncol_num <- .data %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
# return the new variable
return(ncol_num)
}
fires %>% count_ncol_numeric()
## [1] 5
fires %>% .[1:10, 1:5] %>% count_ncol_numeric()
## [1] 3
count_ncol_type <- function(.data, type_data = "numeric") {
# if statement for type of variable
if(type_data == "numeric") {
# body
ncol_type <- .data %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
} else if(type_data == "character") {
#body
ncol_type <- .data %>%
# Select a type of variables
select(where(is.character)) %>%
# Count columns
ncol()
}
# return the new variable
return(ncol_type)
}
fires %>% count_ncol_type()
## [1] 5
fires %>% count_ncol_type(type_data = "character")
## [1] 2
fires %>% .[1:10, 1:5] %>% count_ncol_type(type_data = "character")
## [1] 2
nrow_num <- fires %>%
# filter rows that meet a condition
filter(SEVERITY == "Mild") %>%
# Count rows
nrow()
nrow_num
## [1] 40
count_num_fires_by_severity <- function(.data, severity_name) {
# body
nrow_num <- .data %>%
# filter rows that meet a condition
filter(SEVERITY == severity_name) %>%
# Count rows
nrow()
# return the new varibale
return(nrow_num)
}
fires %>% count_num_fires_by_severity(severity_name = "Severe")
## [1] 9
Use the filter() function to select rows that meet a condition. Refer to Chapter 5.2 Filter rows with filter()