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…

Repeat the same operation over different columns of a data frame

Case of numeric variables

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

map_dbl(numeric_data, mean, na.rm = TRUE)
##       volcano_number...1                 latitude                longitude 
##             2.985853e+05             1.498468e+01             2.353747e+01 
##                elevation   population_within_5_km  population_within_10_km 
##             1.867027e+03             4.786046e+04             6.121775e+04 
##  population_within_30_km Population_within_100_km 
##             3.040444e+05             2.730174e+06

Create your own function

calculate_mean <- function(x) {
  mean(x, na.rm = TRUE)
}

map_dbl(.x = numeric_data, .f = calculate_mean)
##       volcano_number...1                 latitude                longitude 
##             2.985853e+05             1.498468e+01             2.353747e+01 
##                elevation   population_within_5_km  population_within_10_km 
##             1.867027e+03             4.786046e+04             6.121775e+04 
##  population_within_30_km Population_within_100_km 
##             3.040444e+05             2.730174e+06

Repeat the same operation over different elements of a list

When you have a grouping variable (factor)

volcano_list <- split(volcano, volcano$primary_volcano_type)

map_dbl(.x = volcano_list, .f = ~mean(.x$elevation, na.rm = TRUE))
##             Caldera          Caldera(s)             Complex         Complex(es) 
##           1542.3846           1738.8889           2066.5000           1158.0000 
##            Compound         Crater rows     Fissure vent(s)           Lava cone 
##           2686.7778            422.6000            948.1667           1855.0000 
##       Lava cone(es)        Lava cone(s)           Lava dome        Lava dome(s) 
##           3109.0000           3290.0000           3231.0000           1610.1923 
##             Maar(s)    Pyroclastic cone Pyroclastic cone(s)  Pyroclastic shield 
##           1028.6250           2100.7500           1567.3571           2155.5714 
##              Shield           Shield(s)       Stratovolcano      Stratovolcano? 
##           1670.3294           1541.3333           2206.2521           1563.0000 
##   Stratovolcano(es)          Subglacial           Submarine           Tuff cone 
##           2310.4953           1518.0000           -741.4815            180.0000 
##        Tuff cone(s)      Volcanic field 
##           1192.1429           1704.7887

Create your own

count_volcanoes <- function(x) {
  nrow(x)
}

map_int(.x = volcano_list, .f = count_volcanoes)
##             Caldera          Caldera(s)             Complex         Complex(es) 
##                  65                   9                  46                   1 
##            Compound         Crater rows     Fissure vent(s)           Lava cone 
##                   9                   5                  12                   1 
##       Lava cone(es)        Lava cone(s)           Lava dome        Lava dome(s) 
##                   1                   1                   3                  26 
##             Maar(s)    Pyroclastic cone Pyroclastic cone(s)  Pyroclastic shield 
##                   8                   4                  70                   7 
##              Shield           Shield(s)       Stratovolcano      Stratovolcano? 
##                  85                  33                 353                   1 
##   Stratovolcano(es)          Subglacial           Submarine           Tuff cone 
##                 107                   5                  27                   1 
##        Tuff cone(s)      Volcanic field 
##                   7                  71