Example 1: count columns
code snippets
ncol_num <- flights %>%
# Select a type of variables
select(where(is.numeric)) %>%
# Count columns
ncol()
ncol_num
## [1] 14
Turn them into a 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 arguments for details of operation
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)
}
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
Import data
data <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-01-12/artwork.csv')
## Rows: 69201 Columns: 20
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (12): accession_number, artist, artistRole, title, dateText, medium, cre...
## dbl (7): id, artistId, year, acquisitionYear, width, height, depth
## lgl (1): thumbnailCopyright
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data
## # A tibble: 69,201 × 20
## id accession_number artist artistRole artistId title dateText medium
## <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> <chr>
## 1 1035 A00001 Blake, Robe… artist 38 A Fi… date no… Water…
## 2 1036 A00002 Blake, Robe… artist 38 Two … date no… Graph…
## 3 1037 A00003 Blake, Robe… artist 38 The … ?c.1785 Graph…
## 4 1038 A00004 Blake, Robe… artist 38 Six … date no… Graph…
## 5 1039 A00005 Blake, Will… artist 39 The … 1826–7,… Line …
## 6 1040 A00006 Blake, Will… artist 39 Ciam… 1826–7,… Line …
## 7 1041 A00007 Blake, Will… artist 39 The … 1826–7,… Line …
## 8 1042 A00008 Blake, Will… artist 39 The … 1826–7,… Line …
## 9 1043 A00009 Blake, Will… artist 39 The … 1826–7,… Line …
## 10 1044 A00010 Blake, Will… artist 39 The … 1826–7,… Line …
## # ℹ 69,191 more rows
## # ℹ 12 more variables: creditLine <chr>, year <dbl>, acquisitionYear <dbl>,
## # dimensions <chr>, width <dbl>, height <dbl>, depth <dbl>, units <chr>,
## # inscription <chr>, thumbnailCopyright <lgl>, thumbnailUrl <chr>, url <chr>
Example 3: count rows
code snippets
nrow_role <- data %>%
# filter rows that meet a condition
filter(artistRole == "artist") %>%
# Count rows
nrow()
nrow_role
## [1] 66907
Turn them into a function
count_num_artists_by_artistRole <- function(.data, artist_title) {
# body
nrow_role <- .data %>%
# filter rows that meet a condition
filter(artistRole == artist_title) %>%
# Count rows
nrow()
# return the new vairable
return(nrow_role)
}
data %>% .[1:10, "artistRole"] %>%
count_num_artists_by_artistRole(artist_title = "pseduo")
## [1] 0
data %>% .[1:10, "artistRole"] %>%
count_num_artists_by_artistRole(artist_title = "artist")
## [1] 10