dog <- read_excel("../01_module4/data/myData.xlsx")
dog %>% skimr::skim()
| Name | Piped data |
| Number of rows | 195 |
| Number of columns | 18 |
| _______________________ | |
| Column type frequency: | |
| character | 3 |
| numeric | 15 |
| ________________________ | |
| Group variables | None |
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| Breed | 0 | 1 | 4 | 37 | 0 | 195 | 0 |
| Type | 0 | 1 | 4 | 12 | 0 | 10 | 0 |
| Length | 0 | 1 | 4 | 12 | 0 | 4 | 0 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| Column1 | 0 | 1 | 98.00 | 56.44 | 1 | 49.5 | 98 | 146.5 | 195 | ▇▇▇▇▇ |
| Affectionate With Family | 0 | 1 | 4.48 | 0.86 | 0 | 4.0 | 5 | 5.0 | 5 | ▁▁▂▂▇ |
| Good With Young Children | 0 | 1 | 3.87 | 1.03 | 0 | 3.0 | 3 | 5.0 | 5 | ▁▁▇▁▆ |
| Good With Other Dogs | 0 | 1 | 3.51 | 0.97 | 0 | 3.0 | 3 | 4.0 | 5 | ▁▁▇▂▃ |
| Shedding Level | 0 | 1 | 2.59 | 0.87 | 0 | 2.0 | 3 | 3.0 | 5 | ▂▃▇▁▁ |
| Coat Grooming Frequency | 0 | 1 | 2.28 | 0.96 | 0 | 2.0 | 2 | 3.0 | 5 | ▃▇▅▂▁ |
| Drooling Level | 0 | 1 | 1.79 | 0.96 | 0 | 1.0 | 2 | 2.0 | 5 | ▇▆▂▁▁ |
| Openness To Strangers | 0 | 1 | 3.47 | 0.92 | 0 | 3.0 | 3 | 4.0 | 5 | ▁▁▇▃▂ |
| Playfulness Level | 0 | 1 | 3.63 | 0.74 | 0 | 3.0 | 4 | 4.0 | 5 | ▁▁▇▇▂ |
| Watchdog/Protective Nature | 0 | 1 | 3.72 | 0.96 | 0 | 3.0 | 4 | 5.0 | 5 | ▁▁▇▅▅ |
| Adaptability Level | 0 | 1 | 3.77 | 0.67 | 0 | 3.0 | 4 | 4.0 | 5 | ▁▁▅▇▂ |
| Trainability Level | 0 | 1 | 3.85 | 0.90 | 0 | 3.0 | 4 | 5.0 | 5 | ▁▁▇▇▆ |
| Energy Level | 0 | 1 | 3.72 | 0.80 | 0 | 3.0 | 4 | 4.0 | 5 | ▁▁▇▇▃ |
| Barking Level | 0 | 1 | 3.12 | 1.12 | 0 | 3.0 | 3 | 4.0 | 5 | ▂▁▇▃▂ |
| Mental Stimulation Needs | 0 | 1 | 3.66 | 0.74 | 0 | 3.0 | 4 | 4.0 | 5 | ▁▁▇▇▂ |
ncol_num <- dog %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
ncol_num
## [1] 15
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)
}
dog %>% count_ncol_numeric()
## [1] 15
dog %>% .[1:10, -1:-13] %>% count_ncol_numeric()
## [1] 5
count_ncol_type <- 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)
}
dog %>% count_ncol_type()
## [1] 15
dog %>% count_ncol_type(type_data = "character")
## [1] 3
dog %>% .[1:10, 1:5] %>% count_ncol_type(type_data = "character")
## [1] 1
nrow_num <- dog %>%
# filter rows that meet a condition
filter(Length == "Long") %>%
# Count rows
nrow()
nrow_num
## [1] 29
count_num_dogs_per_fur_length <- function(.data, fur_length) {
# body
nrow_num <- .data %>%
# filter rows that meet a condition
filter(Length == fur_length) %>%
# Count rows
nrow()
# return the new variable
return(nrow_num)
}
dog %>% .[1:10, "Length"] %>% count_num_dogs_per_fur_length(fur_length = "Short")
## [1] 7
Create your own.
Use the filter() function to select rows that meet a condition. Refer to Chapter 5.2 Filter rows with filter()
nrow_num <- dog %>%
# filter rows that meet a condition
filter(Type == "Double") %>%
# Count rows
nrow()
nrow_num
## [1] 66
count_num_dogs_per_fur_type <- function(.data, fur_type) {
# body
nrow_num <- .data %>%
# filter rows that meet a condition
filter(Type == fur_type) %>%
# Count rows
nrow()
# return the new variable
return(nrow_num)
}
dog %>% .[1:10, "Type"] %>% count_num_dogs_per_fur_type(fur_type = "Double")
## [1] 3