data(penguins)
penguins %>% skimr::skim()
| Name | Piped data |
| Number of rows | 344 |
| Number of columns | 8 |
| _______________________ | |
| Column type frequency: | |
| factor | 3 |
| numeric | 5 |
| ________________________ | |
| Group variables | None |
Variable type: factor
| skim_variable | n_missing | complete_rate | ordered | n_unique | top_counts |
|---|---|---|---|---|---|
| species | 0 | 1.00 | FALSE | 3 | Ade: 152, Gen: 124, Chi: 68 |
| island | 0 | 1.00 | FALSE | 3 | Bis: 168, Dre: 124, Tor: 52 |
| sex | 11 | 0.97 | FALSE | 2 | mal: 168, fem: 165 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| bill_len | 2 | 0.99 | 43.92 | 5.46 | 32.1 | 39.23 | 44.45 | 48.5 | 59.6 | ▃▇▇▆▁ |
| bill_dep | 2 | 0.99 | 17.15 | 1.97 | 13.1 | 15.60 | 17.30 | 18.7 | 21.5 | ▅▅▇▇▂ |
| flipper_len | 2 | 0.99 | 200.92 | 14.06 | 172.0 | 190.00 | 197.00 | 213.0 | 231.0 | ▂▇▃▅▂ |
| body_mass | 2 | 0.99 | 4201.75 | 801.95 | 2700.0 | 3550.00 | 4050.00 | 4750.0 | 6300.0 | ▃▇▆▃▂ |
| year | 0 | 1.00 | 2008.03 | 0.82 | 2007.0 | 2007.00 | 2008.00 | 2009.0 | 2009.0 | ▇▁▇▁▇ |
penguins <- penguins %>%
mutate(across(where(is.factor), as.character))
ncol_num <- penguins %>%
# 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)
}
penguins %>% count_ncol_numeric()
## [1] 5
penguins %>% .[1:10, 1:5] %>% count_ncol_numeric()
## [1] 3
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)
}
penguins %>% count_ncol_type()
## [1] 5
penguins %>% count_ncol_type(type_data = "character")
## [1] 3
penguins %>% .[1:10, 1:5] %>% count_ncol_type(type_data = "character")
## [1] 2
nrow_num <- penguins %>%
# filter rows that meet a condition
filter(island == "Torgersen") %>%
# Count rows
nrow()
nrow_num
## [1] 52
count_num_penguins_by_island <- function(.data ,island_name) {
# Body
nrow_num <- .data %>%
# filter rows that meet a condition
filter(island == island_name) %>%
# count rows
nrow()
# return the new variable
return(nrow_num)
}
penguins %>% .[1:10, "island", drop = FALSE] %>%
count_num_penguins_by_island(island_name = "Torgersen")
## [1] 10
Use the filter() function to select rows that meet a condition. Refer to Chapter 5.2 Filter rows with filter()
nrow_num <- penguins %>%
# filter rows that meet a condition
filter(island == "Torgersen") %>%
# Count rows
nrow()
nrow_num
## [1] 52
count_num_penguins_by_island <- function(.data ,island_name) {
# Body
nrow_num <- .data %>%
# filter rows that meet a condition
filter(island == island_name) %>%
# count rows
nrow()
# return the new variable
return(nrow_num)
}
penguins %>% .[1:10, "island", drop = FALSE] %>%
count_num_penguins_by_island(island_name = "Torgersen")
## [1] 10