Import your data

volcano <- read_csv(
  "/Users/isabelk/Desktop/PSU_DAT3000_IntroToDA/06_module9/volcanos.csv",
  skip = 1,
  trim_ws = TRUE
)
## New names:
## Rows: 958 Columns: 35
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (17): volcano_name, primary_volcano_type, last_eruption_year, country, r... dbl
## (8): volcano_number...1, latitude, longitude, elevation, population_wit... lgl
## (10): volcano_number...22, volcano_number...27, ...28, ...29, ...30, ......
## ℹ 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.
## • `volcano_number` -> `volcano_number...1`
## • `` -> `...13`
## • `` -> `...19`
## • `volcano_number` -> `volcano_number...20`
## • `volcano_number` -> `volcano_number...21`
## • `volcano_number` -> `volcano_number...22`
## • `volcano_number` -> `volcano_number...27`
## • `` -> `...28`
## • `` -> `...29`
## • `` -> `...30`
## • `` -> `...31`
## • `` -> `...32`
## • `` -> `...33`
## • `` -> `...34`
## • `` -> `...35`
names(volcano) <- str_trim(names(volcano))

glimpse(volcano)
## Rows: 958
## Columns: 35
## $ volcano_number...1       <dbl> 283001, 355096, 342080, 213004, 321040, 28317…
## $ volcano_name             <chr> "Abu", "Acamarachi", "Acatenango", "Acigol-Ne…
## $ primary_volcano_type     <chr> "Shield(s)", "Stratovolcano", "Stratovolcano(…
## $ last_eruption_year       <chr> "-6850", "Unknown", "1972", "-2080", "950", "…
## $ country                  <chr> "Japan", "Chile", "Guatemala", "Turkey", "Uni…
## $ region                   <chr> "Japan, Taiwan, Marianas", "South America", "…
## $ subregion                <chr> "Honshu", "Northern Chile, Bolivia and Argent…
## $ latitude                 <dbl> 34.500, -23.292, 14.501, 38.537, 46.206, 37.6…
## $ longitude                <dbl> 131.600, -67.618, -90.876, 34.621, -121.490, …
## $ elevation                <dbl> 641, 6023, 3976, 1683, 3742, 1728, 1733, 1250…
## $ tectonic_settings        <chr> "Subduction zone / Continental crust (>25 km)…
## $ evidence_category        <chr> "Eruption Dated", "Evidence Credible", "Erupt…
## $ ...13                    <chr> "Andesite / Basaltic Andesite", "Dacite", "An…
## $ major_rock_1             <chr> "Basalt / Picro-Basalt", "Andesite / Basaltic…
## $ major_rock_2             <chr> "Dacite", NA, NA, "Basalt / Picro-Basalt", NA…
## $ major_rock_3             <chr> NA, NA, NA, "Andesite / Basaltic Andesite", N…
## $ major_rock_4             <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ major_rock_5             <chr> NA, NA, "Basalt / Picro-Basalt", NA, "Dacite"…
## $ ...19                    <chr> NA, NA, NA, NA, NA, "Basalt / Picro-Basalt", …
## $ volcano_number...20      <chr> NA, NA, NA, NA, NA, NA, NA, "Andesite / Basal…
## $ volcano_number...21      <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ volcano_number...22      <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ population_within_5_km   <dbl> 3597, 0, 4329, 127863, 0, 428, 101, 51, 0, 98…
## $ population_within_10_km  <dbl> 9594, 7, 60730, 127863, 70, 3936, 485, 6042, …
## $ population_within_30_km  <dbl> 117805, 294, 1042836, 218469, 4019, 717078, 1…
## $ Population_within_100_km <dbl> 4071152, 9092, 7634778, 2253483, 393303, 5024…
## $ volcano_number...27      <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ ...28                    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ ...29                    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ ...30                    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ ...31                    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ ...32                    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ ...33                    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ ...34                    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ ...35                    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…

Example 1: Count columns

Code snippets

ncol_num <- volcano %>%
  select(where(is.numeric)) %>%
  ncol()

ncol_num
## [1] 8

Turn them into a function

count_numeric_columns <- function(data) {

  result <- data %>%
    select(where(is.numeric)) %>%
    ncol()

  return(result)
}

Test the function

count_numeric_columns(volcano)
## [1] 8
count_numeric_columns(volcano[, 1:5])
## [1] 1
count_numeric_columns(volcano[, 1:10])
## [1] 4

Adding arguments for details of operation

count_columns_by_type <- function(data, type_data = "numeric") {

  if (type_data == "numeric") {

    result <- data %>%
      select(where(is.numeric)) %>%
      ncol()

  } else if (type_data == "character") {

    result <- data %>%
      select(where(is.character)) %>%
      ncol()

  } else {
    stop("Please choose numeric or character")
  }

  return(result)
}

Test the function

count_columns_by_type(volcano)
## [1] 8
count_columns_by_type(volcano, type_data = "character")
## [1] 17
count_columns_by_type(volcano[, 1:5])
## [1] 1

Example 2: Count rows

Code snippets

nrow_type <- volcano %>%

  filter(primary_volcano_type == "Stratovolcano") %>%

  nrow()

nrow_type
## [1] 353

Turn them into a function

count_volcano_by_type <- function(data, type_name) {

  result <- data %>%
    filter(primary_volcano_type == type_name) %>%
    nrow()

  return(result)
}

Test the function

count_volcano_by_type(volcano, "Stratovolcano")
## [1] 353
count_volcano_by_type(volcano, "Shield")
## [1] 85

Example 3: Count rows using your own example

Code snippets

nrow_country <- volcano %>%

  filter(country == "United States") %>%

  nrow()

nrow_country
## [1] 99

Turn them into a function

count_volcano_by_country <- function(data, country_name) {

  result <- data %>%
    filter(country == country_name) %>%
    nrow()

  return(result)
}

Test the function

count_volcano_by_country(volcano, "United States")
## [1] 99
count_volcano_by_country(volcano, "Japan")
## [1] 92