# Load the volcano dataset
volcano_file <- "/Users/isabelk/Desktop/PSU_DAT3000_IntroToDA/06_module9/volcanos.csv"
# Read and clean the file
volcano_lines <- readLines(volcano_file, warn = FALSE)
# Remove the extra title row
volcano_lines <- volcano_lines[-1]
# Remove the outside quotation marks
volcano_lines <- str_remove(volcano_lines, '^"')
volcano_lines <- str_remove(volcano_lines, '"$')
# Remove extra tabs
volcano_lines <- str_replace_all(volcano_lines, "\t", "")
# Import the cleaned data
volcano <- read_csv(
I(paste(volcano_lines, collapse = "\n")),
show_col_types = FALSE
)
## New names:
## • `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`
# Check the data
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…
# Remove empty and duplicate extra columns
volcano <- volcano %>%
select(
volcano_number = volcano_number...1,
volcano_name,
primary_volcano_type,
last_eruption_year,
country,
region,
subregion,
latitude,
longitude,
elevation,
tectonic_settings,
evidence_category,
major_rock_1,
major_rock_2,
major_rock_3,
major_rock_4,
major_rock_5,
population_within_5_km,
population_within_10_km,
population_within_30_km,
Population_within_100_km
)
# Create a factor for volcano type
volcano <- volcano %>%
mutate(primary_volcano_type = as.factor(primary_volcano_type))
# View volcano types
volcano %>%
distinct(primary_volcano_type)
## # A tibble: 26 × 1
## primary_volcano_type
## <fct>
## 1 Shield(s)
## 2 Stratovolcano
## 3 Stratovolcano(es)
## 4 Caldera
## 5 Submarine
## 6 Shield
## 7 Volcanic field
## 8 Fissure vent(s)
## 9 Compound
## 10 Complex
## # ℹ 16 more rows
# Count volcano types
volcano_by_type <- volcano %>%
count(primary_volcano_type, sort = TRUE)
volcano_by_type
## # A tibble: 26 × 2
## primary_volcano_type n
## <fct> <int>
## 1 Stratovolcano 353
## 2 Stratovolcano(es) 107
## 3 Shield 85
## 4 Volcanic field 71
## 5 Pyroclastic cone(s) 70
## 6 Caldera 65
## 7 Complex 46
## 8 Shield(s) 33
## 9 Submarine 27
## 10 Lava dome(s) 26
## # ℹ 16 more rows
Bar chart before ordering
volcano_by_type %>%
ggplot(aes(x = primary_volcano_type, y = n)) +
geom_col() +
coord_flip() +
labs(
title = "Number of Volcanoes by Type",
x = "Volcano Type",
y = "Number of Volcanoes"
)
Bar chart after ordering
volcano_by_type %>%
ggplot(aes(
x = fct_reorder(primary_volcano_type, n),
y = n
)) +
geom_col() +
coord_flip() +
labs(
title = "Volcano Types Ordered by Frequency",
x = "Volcano Type",
y = "Number of Volcanoes"
)
1. fct_recode()
# Rename volcano type levels
volcano %>%
mutate(
type_recode = fct_recode(
primary_volcano_type,
"Stratovolcano" = "Stratovolcano(es)"
)
) %>%
select(primary_volcano_type, type_recode) %>%
distinct()
## # A tibble: 26 × 2
## primary_volcano_type type_recode
## <fct> <fct>
## 1 Shield(s) Shield(s)
## 2 Stratovolcano Stratovolcano
## 3 Stratovolcano(es) Stratovolcano
## 4 Caldera Caldera
## 5 Submarine Submarine
## 6 Shield Shield
## 7 Volcanic field Volcanic field
## 8 Fissure vent(s) Fissure vent(s)
## 9 Compound Compound
## 10 Complex Complex
## # ℹ 16 more rows
2. fct_collapse()
# Collapse similar volcano types into one category
volcano %>%
mutate(
type_collapsed = fct_collapse(
primary_volcano_type,
"Stratovolcano" = c("Stratovolcano", "Stratovolcano(es)")
)
) %>%
select(primary_volcano_type, type_collapsed) %>%
distinct()
## # A tibble: 26 × 2
## primary_volcano_type type_collapsed
## <fct> <fct>
## 1 Shield(s) Shield(s)
## 2 Stratovolcano Stratovolcano
## 3 Stratovolcano(es) Stratovolcano
## 4 Caldera Caldera
## 5 Submarine Submarine
## 6 Shield Shield
## 7 Volcanic field Volcanic field
## 8 Fissure vent(s) Fissure vent(s)
## 9 Compound Compound
## 10 Complex Complex
## # ℹ 16 more rows
3. fct_lump()
# Lump less common volcano types into Other
volcano %>%
mutate(
type_lumped = fct_lump_n(primary_volcano_type, n = 5)
) %>%
count(type_lumped, sort = TRUE)
## # A tibble: 6 × 2
## type_lumped n
## <fct> <int>
## 1 Stratovolcano 353
## 2 Other 272
## 3 Stratovolcano(es) 107
## 4 Shield 85
## 5 Volcanic field 71
## 6 Pyroclastic cone(s) 70
No additional work required.