data("diamonds")
diamonds %>% skimr::skim()
| Name | Piped data |
| Number of rows | 53940 |
| Number of columns | 10 |
| _______________________ | |
| Column type frequency: | |
| factor | 3 |
| numeric | 7 |
| ________________________ | |
| Group variables | None |
Variable type: factor
| skim_variable | n_missing | complete_rate | ordered | n_unique | top_counts |
|---|---|---|---|---|---|
| cut | 0 | 1 | TRUE | 5 | Ide: 21551, Pre: 13791, Ver: 12082, Goo: 4906 |
| color | 0 | 1 | TRUE | 7 | G: 11292, E: 9797, F: 9542, H: 8304 |
| clarity | 0 | 1 | TRUE | 8 | SI1: 13065, VS2: 12258, SI2: 9194, VS1: 8171 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| carat | 0 | 1 | 0.80 | 0.47 | 0.2 | 0.40 | 0.70 | 1.04 | 5.01 | ▇▂▁▁▁ |
| depth | 0 | 1 | 61.75 | 1.43 | 43.0 | 61.00 | 61.80 | 62.50 | 79.00 | ▁▁▇▁▁ |
| table | 0 | 1 | 57.46 | 2.23 | 43.0 | 56.00 | 57.00 | 59.00 | 95.00 | ▁▇▁▁▁ |
| price | 0 | 1 | 3932.80 | 3989.44 | 326.0 | 950.00 | 2401.00 | 5324.25 | 18823.00 | ▇▂▁▁▁ |
| x | 0 | 1 | 5.73 | 1.12 | 0.0 | 4.71 | 5.70 | 6.54 | 10.74 | ▁▁▇▃▁ |
| y | 0 | 1 | 5.73 | 1.14 | 0.0 | 4.72 | 5.71 | 6.54 | 58.90 | ▇▁▁▁▁ |
| z | 0 | 1 | 3.54 | 0.71 | 0.0 | 2.91 | 3.53 | 4.04 | 31.80 | ▇▁▁▁▁ |
ncol_num <- flights %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
ncol_num
## [1] 14
count_ncol_numeric <- function(.data) {
# body
ncol_num <- flights %>%
#Select type of variable
select(where(is.numeric)) %>%
#count columns
ncol()
#Return
return(ncol_num)
}
flights %>% count_ncol_numeric()
## [1] 14
flights %>% .[1:10,-1:-13] %>% count_ncol_numeric()
## [1] 14
count_ncol_type <- function(.data, type_data = "numeric") {
# If Statement
if(type_data == "numeric") {
# body
ncol_type <- .data %>%
#Select type of variable
select(where(is.numeric)) %>%
#count columns
ncol()
} else if(type_data == "character") {
# body
ncol_type <- .data %>%
#Select type of variable
select(where(is.character)) %>%
#count columns
ncol()
}
#Return
return(ncol_type)
}
flights %>% count_ncol_type()
## [1] 14
flights %>% count_ncol_type(type_data = "character")
## [1] 4
flights %>% .[1:10,1:5] %>% count_ncol_type(type_data = "character")
## [1] 0
nrow_num <- flights %>%
# filter rows that meet a condition
filter(carrier == "UA") %>%
# Count rows
nrow()
nrow_num
## [1] 58665
count_num_flights <- function(.data, carrier_name) {
#body
nrow_num <- .data %>%
# filter rows that meet a condition
filter(carrier == carrier_name) %>%
# Count rows
nrow()
# Return
return(nrow_num)
}
flights %>% .[1:10, "carrier"] %>% count_num_flights(carrier_name = "UA")
## [1] 3
Create your own.
nrows_num <- diamonds %>%
# Select a type of variables
select() %>%
# Count rows
nrow()
nrows_num
## [1] 53940
Use the filter() function to select rows that meet a condition. Refer to Chapter 5.2 Filter rows with filter()
nrows_high_carat <- function(.data) {
#Body
nrows_carat <- diamonds %>%
filter(cut == "Very Good") %>%
#count
nrow()
#Return
return(nrows_carat)
}
diamonds %>% nrows_high_carat()
## [1] 12082