data(flights)
flights %>% skimr::skim()
| Name | Piped data |
| Number of rows | 336776 |
| Number of columns | 19 |
| _______________________ | |
| Column type frequency: | |
| character | 4 |
| numeric | 14 |
| POSIXct | 1 |
| ________________________ | |
| Group variables | None |
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| carrier | 0 | 1.00 | 2 | 2 | 0 | 16 | 0 |
| tailnum | 2512 | 0.99 | 5 | 6 | 0 | 4043 | 0 |
| origin | 0 | 1.00 | 3 | 3 | 0 | 3 | 0 |
| dest | 0 | 1.00 | 3 | 3 | 0 | 105 | 0 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| year | 0 | 1.00 | 2013.00 | 0.00 | 2013 | 2013 | 2013 | 2013 | 2013 | ▁▁▇▁▁ |
| month | 0 | 1.00 | 6.55 | 3.41 | 1 | 4 | 7 | 10 | 12 | ▇▆▆▆▇ |
| day | 0 | 1.00 | 15.71 | 8.77 | 1 | 8 | 16 | 23 | 31 | ▇▇▇▇▆ |
| dep_time | 8255 | 0.98 | 1349.11 | 488.28 | 1 | 907 | 1401 | 1744 | 2400 | ▁▇▆▇▃ |
| sched_dep_time | 0 | 1.00 | 1344.25 | 467.34 | 106 | 906 | 1359 | 1729 | 2359 | ▁▇▇▇▃ |
| dep_delay | 8255 | 0.98 | 12.64 | 40.21 | -43 | -5 | -2 | 11 | 1301 | ▇▁▁▁▁ |
| arr_time | 8713 | 0.97 | 1502.05 | 533.26 | 1 | 1104 | 1535 | 1940 | 2400 | ▁▃▇▇▇ |
| sched_arr_time | 0 | 1.00 | 1536.38 | 497.46 | 1 | 1124 | 1556 | 1945 | 2359 | ▁▃▇▇▇ |
| arr_delay | 9430 | 0.97 | 6.90 | 44.63 | -86 | -17 | -5 | 14 | 1272 | ▇▁▁▁▁ |
| flight | 0 | 1.00 | 1971.92 | 1632.47 | 1 | 553 | 1496 | 3465 | 8500 | ▇▃▃▁▁ |
| air_time | 9430 | 0.97 | 150.69 | 93.69 | 20 | 82 | 129 | 192 | 695 | ▇▂▂▁▁ |
| distance | 0 | 1.00 | 1039.91 | 733.23 | 17 | 502 | 872 | 1389 | 4983 | ▇▃▂▁▁ |
| hour | 0 | 1.00 | 13.18 | 4.66 | 1 | 9 | 13 | 17 | 23 | ▁▇▇▇▅ |
| minute | 0 | 1.00 | 26.23 | 19.30 | 0 | 8 | 29 | 44 | 59 | ▇▃▆▃▅ |
Variable type: POSIXct
| skim_variable | n_missing | complete_rate | min | max | median | n_unique |
|---|---|---|---|---|---|---|
| time_hour | 0 | 1 | 2013-01-01 05:00:00 | 2013-12-31 23:00:00 | 2013-07-03 10:00:00 | 6936 |
ncol_num <- flights %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
ncol_num
## [1] 14
count_ncol_numeric <- function(.data) {
ncol_num <- .data %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
# return the new variable
return(ncol_num)
}
flights %>% count_ncol_numeric()
## [1] 14
flights %>% .[1:10, -1:-13]
## # A tibble: 10 × 6
## dest air_time distance hour minute time_hour
## <chr> <dbl> <dbl> <dbl> <dbl> <dttm>
## 1 IAH 227 1400 5 15 2013-01-01 05:00:00
## 2 IAH 227 1416 5 29 2013-01-01 05:00:00
## 3 MIA 160 1089 5 40 2013-01-01 05:00:00
## 4 BQN 183 1576 5 45 2013-01-01 05:00:00
## 5 ATL 116 762 6 0 2013-01-01 06:00:00
## 6 ORD 150 719 5 58 2013-01-01 05:00:00
## 7 FLL 158 1065 6 0 2013-01-01 06:00:00
## 8 IAD 53 229 6 0 2013-01-01 06:00:00
## 9 MCO 140 944 6 0 2013-01-01 06:00:00
## 10 ORD 138 733 6 0 2013-01-01 06:00:00
count_ncol_type<- function(.data, type_data = "numeric") {
# if statement for type of variables
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 the new variable
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 == "DL") %>%
# Count rows
nrow()
nrow_num
## [1] 48110
count_num_flights_by_carrier <- function(.data, carrier_name) {
# body
nrow_num <- .data %>%
# filter rows that meet a condition
filter(carrier == carrier_name) %>%
# Count rows
nrow()
# return the new variable
return(nrow_num)
}
flights %>% head(10) %>% count_num_flights_by_carrier(carrier_name = "AA")
## [1] 2
Create your own. ## import data
data(lakers)
lakers %>% skimr::skim()
| Name | Piped data |
| Number of rows | 34624 |
| Number of columns | 13 |
| _______________________ | |
| Column type frequency: | |
| character | 8 |
| numeric | 5 |
| ________________________ | |
| Group variables | None |
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| opponent | 0 | 1 | 3 | 3 | 0 | 29 | 0 |
| game_type | 0 | 1 | 4 | 4 | 0 | 2 | 0 |
| time | 0 | 1 | 5 | 5 | 0 | 717 | 0 |
| etype | 0 | 1 | 3 | 10 | 0 | 10 | 0 |
| team | 0 | 1 | 3 | 3 | 0 | 31 | 0 |
| player | 0 | 1 | 0 | 24 | 5393 | 371 | 0 |
| result | 0 | 1 | 0 | 6 | 17736 | 3 | 0 |
| type | 0 | 1 | 0 | 25 | 10382 | 74 | 0 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| date | 0 | 1.00 | 20086943.97 | 4372.40 | 20081028 | 20081210 | 20090121 | 20090309 | 20090414 | ▅▁▁▁▇ |
| period | 0 | 1.00 | 2.54 | 1.12 | 1 | 2 | 3 | 4 | 5 | ▇▇▇▇▁ |
| points | 0 | 1.00 | 0.46 | 0.85 | 0 | 0 | 0 | 1 | 3 | ▇▁▁▂▁ |
| x | 21557 | 0.38 | 25.32 | 11.19 | 0 | 20 | 25 | 31 | 51 | ▂▂▇▂▂ |
| y | 21557 | 0.38 | 13.43 | 8.67 | 3 | 6 | 10 | 20 | 90 | ▇▂▁▁▁ |
nrow_num <- lakers %>%
# filter rows that meet a condition
filter(player == "Kobe Bryant") %>%
# Count rows
nrow()
nrow_num
## [1] 2945
Use the filter() function to select rows that meet a condition. Refer to Chapter 5.2 Filter rows with filter()
count_num_made_shots_by_player <- function(.data, player_name) {
# body
nrow_num <- .data %>%
# filter rows that meet a condition
filter(player == player_name, result == "made") %>%
# Count rows
nrow()
return(nrow_num)
}
count_num_made_shots_by_player(lakers, player_name = "Kobe Bryant")
## [1] 1206
count_num_made_shots_by_player(lakers, player_name = "Pau Gasol")
## [1] 902
count_num_made_shots_by_player(lakers, player_name = "Derek Fisher")
## [1] 369
count_num_made_shots_by_player(lakers, player_name = "Lamar Odom")
## [1] 484