Import your data

volcano <- read_csv(
  "/Users/isabelk/Desktop/PSU_DAT3000_IntroToDA/06_module9/volcanos.csv",
  skip = 1
)
## 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)
##  [1] "volcano_number...1"       "volcano_name"            
##  [3] "primary_volcano_type"     "last_eruption_year"      
##  [5] "country"                  "region"                  
##  [7] "subregion"                "latitude"                
##  [9] "longitude"                "elevation"               
## [11] "tectonic_settings"        "evidence_category"       
## [13] "...13"                    "major_rock_1"            
## [15] "major_rock_2"             "major_rock_3"            
## [17] "major_rock_4"             "major_rock_5"            
## [19] "...19"                    "volcano_number...20"     
## [21] "volcano_number...21"      "volcano_number...22"     
## [23] "population_within_5_km"   "population_within_10_km" 
## [25] "population_within_30_km"  "Population_within_100_km"
## [27] "volcano_number...27"      "...28"                   
## [29] "...29"                    "...30"                   
## [31] "...31"                    "...32"                   
## [33] "...33"                    "...34"                   
## [35] "...35"

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

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

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