Import your data
data(flights)
flights %>% skimr::skim()
Data summary
| 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
| 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
| 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
| time_hour |
0 |
1 |
2013-01-01 05:00:00 |
2013-12-31 23:00:00 |
2013-07-03 10:00:00 |
6936 |
Create Data frame functions
##Example 1
code snippets
ncol_num <- flights %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
ncol_num
## [1] 14
function
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)
}
flights %>% count_ncol_numeric()
## [1] 14
flights %>% .[1:10, -1:-13] %>% count_ncol_numeric()
## [1] 4
adding argumens for deyails of operations
count_ncol_types <- function(.data, type_data = "numeric") {
# if statement for type of variables
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)
}
flights %>% count_ncol_types()
## [1] 14
Example 2: count rows
code snippets
nrow_num <- flights %>%
# filter rows that meet a condition
filter(carrier == "UA") %>%
# Count rows
nrow()
nrow_num
## [1] 58665
Turn them into a function
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 %>% .[1:10, "carrier"] %>% count_num_flights_by_carrier(carrier_name = "AA")
## [1] 2
Example 3: count rows
data(mtcars)
mtcars %>% skimr::skim()
Data summary
| Name |
Piped data |
| Number of rows |
32 |
| Number of columns |
11 |
| _______________________ |
|
| Column type frequency: |
|
| numeric |
11 |
| ________________________ |
|
| Group variables |
None |
Variable type: numeric
| mpg |
0 |
1 |
20.09 |
6.03 |
10.40 |
15.43 |
19.20 |
22.80 |
33.90 |
▃▇▅▁▂ |
| cyl |
0 |
1 |
6.19 |
1.79 |
4.00 |
4.00 |
6.00 |
8.00 |
8.00 |
▆▁▃▁▇ |
| disp |
0 |
1 |
230.72 |
123.94 |
71.10 |
120.83 |
196.30 |
326.00 |
472.00 |
▇▃▃▃▂ |
| hp |
0 |
1 |
146.69 |
68.56 |
52.00 |
96.50 |
123.00 |
180.00 |
335.00 |
▇▇▆▃▁ |
| drat |
0 |
1 |
3.60 |
0.53 |
2.76 |
3.08 |
3.70 |
3.92 |
4.93 |
▇▃▇▅▁ |
| wt |
0 |
1 |
3.22 |
0.98 |
1.51 |
2.58 |
3.33 |
3.61 |
5.42 |
▃▃▇▁▂ |
| qsec |
0 |
1 |
17.85 |
1.79 |
14.50 |
16.89 |
17.71 |
18.90 |
22.90 |
▃▇▇▂▁ |
| vs |
0 |
1 |
0.44 |
0.50 |
0.00 |
0.00 |
0.00 |
1.00 |
1.00 |
▇▁▁▁▆ |
| am |
0 |
1 |
0.41 |
0.50 |
0.00 |
0.00 |
0.00 |
1.00 |
1.00 |
▇▁▁▁▆ |
| gear |
0 |
1 |
3.69 |
0.74 |
3.00 |
3.00 |
4.00 |
4.00 |
5.00 |
▇▁▆▁▂ |
| carb |
0 |
1 |
2.81 |
1.62 |
1.00 |
2.00 |
2.00 |
4.00 |
8.00 |
▇▂▅▁▁ |
code snippets
ncol_numeric <- mtcars %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
ncol_numeric
## [1] 11
Turn them into a function
numeric_count_ncol <- function(.data) {
# body
ncol_numeric <- .data %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
# return the new variable
return(ncol_numeric)
}
mtcars %>% numeric_count_ncol()
## [1] 11