knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Load volcano dataset and skip the extra first row
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`
# Clean up column names
names(volcano) <- str_trim(names(volcano))
# Look at the dataset
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…
# Check column names
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"
Chapter 14: Strings
Working with strings
chac_data <- "I'm 'very' hungry."
chac_data
## [1] "I'm 'very' hungry."
str_length(chac_data)
## [1] 18
Combining strings
str_c("Volcano", "Data")
## [1] "VolcanoData"
str_c("Volcano", "Data", sep = " ")
## [1] "Volcano Data"
Sorting strings
str_sort(c("volcano", "earthquake", "mountain"))
## [1] "earthquake" "mountain" "volcano"
Looking at the volcano dataset
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…
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"
Creating a smaller dataset
volcano_small <- volcano %>%
select(where(is.character)) %>%
head(n = 10)
volcano_small
## # A tibble: 10 × 17
## volcano_name primary_volcano_type last_eruption_year country region subregion
## <chr> <chr> <chr> <chr> <chr> <chr>
## 1 Abu Shield(s) -6850 Japan Japan… Honshu
## 2 Acamarachi Stratovolcano Unknown Chile South… Northern…
## 3 Acatenango Stratovolcano(es) 1972 Guatem… Méxic… Guatemala
## 4 Acigol-Nevs… Caldera -2080 Turkey Medit… Turkey
## 5 Adams Stratovolcano 950 United… Canad… USA (Was…
## 6 Adatarayama Stratovolcano(es) 1996 Japan Japan… Honshu
## 7 Adwa Stratovolcano Unknown Ethiop… Afric… Africa (…
## 8 Afdera Stratovolcano Unknown Ethiop… Afric… Africa (…
## 9 Agrigan Stratovolcano 1917 United… Japan… Izu, Vol…
## 10 Agua Stratovolcano Unknown Guatem… Méxic… Guatemala
## # ℹ 11 more variables: tectonic_settings <chr>, evidence_category <chr>,
## # ...13 <chr>, major_rock_1 <chr>, major_rock_2 <chr>, major_rock_3 <chr>,
## # major_rock_4 <chr>, major_rock_5 <chr>, ...19 <chr>,
## # volcano_number...20 <chr>, volcano_number...21 <chr>
Finding text in the dataset
volcano %>%
filter(str_detect(primary_volcano_type, "Stratovolcano"))
## # A tibble: 461 × 35
## volcano_number...1 volcano_name primary_volcano_type last_eruption_year
## <dbl> <chr> <chr> <chr>
## 1 355096 Acamarachi Stratovolcano Unknown
## 2 342080 Acatenango Stratovolcano(es) 1972
## 3 321040 Adams Stratovolcano 950
## 4 283170 Adatarayama Stratovolcano(es) 1996
## 5 221170 Adwa Stratovolcano Unknown
## 6 221110 Afdera Stratovolcano Unknown
## 7 284160 Agrigan Stratovolcano 1917
## 8 342100 Agua Stratovolcano Unknown
## 9 382090 Agua de Pau Stratovolcano 1564
## 10 358062 Aguilera Stratovolcano -1250
## # ℹ 451 more rows
## # ℹ 31 more variables: country <chr>, region <chr>, subregion <chr>,
## # latitude <dbl>, longitude <dbl>, elevation <dbl>, tectonic_settings <chr>,
## # evidence_category <chr>, ...13 <chr>, major_rock_1 <chr>,
## # major_rock_2 <chr>, major_rock_3 <chr>, major_rock_4 <chr>,
## # major_rock_5 <chr>, ...19 <chr>, volcano_number...20 <chr>,
## # volcano_number...21 <chr>, volcano_number...22 <lgl>, …
Detecting text with regular expressions
volcano %>%
filter(str_detect(country, "United States"))
## # A tibble: 99 × 35
## volcano_number...1 volcano_name primary_volcano_type last_eruption_year
## <dbl> <chr> <chr> <chr>
## 1 321040 Adams Stratovolcano 950
## 2 284160 Agrigan Stratovolcano 1917
## 3 284141 Ahyi Submarine 2014
## 4 311320 Akutan Stratovolcano 1992
## 5 311390 Amak Stratovolcano 1796
## 6 311190 Amukta Stratovolcano 1997
## 7 284200 Anatahan Stratovolcano 2008
## 8 312090 Aniakchak Caldera 1931
## 9 284150 Asuncion Stratovolcano 1906
## 10 311160 Atka Stratovolcano(es) 1812
## # ℹ 89 more rows
## # ℹ 31 more variables: country <chr>, region <chr>, subregion <chr>,
## # latitude <dbl>, longitude <dbl>, elevation <dbl>, tectonic_settings <chr>,
## # evidence_category <chr>, ...13 <chr>, major_rock_1 <chr>,
## # major_rock_2 <chr>, major_rock_3 <chr>, major_rock_4 <chr>,
## # major_rock_5 <chr>, ...19 <chr>, volcano_number...20 <chr>,
## # volcano_number...21 <chr>, volcano_number...22 <lgl>, …
Counting matching text
volcano %>%
summarize(
number_of_volcanoes = sum(str_detect(country, "United States"), na.rm = TRUE)
)
## # A tibble: 1 × 1
## number_of_volcanoes
## <int>
## 1 99
Replacing text
volcano %>%
mutate(
volcano_type_new = str_replace(
primary_volcano_type,
"Stratovolcano",
"Strato"
)
) %>%
select(primary_volcano_type, volcano_type_new)
## # A tibble: 958 × 2
## primary_volcano_type volcano_type_new
## <chr> <chr>
## 1 Shield(s) Shield(s)
## 2 Stratovolcano Strato
## 3 Stratovolcano(es) Strato(es)
## 4 Caldera Caldera
## 5 Stratovolcano Strato
## 6 Stratovolcano(es) Strato(es)
## 7 Stratovolcano Strato
## 8 Stratovolcano Strato
## 9 Stratovolcano Strato
## 10 Stratovolcano Strato
## # ℹ 948 more rows
Splitting strings
str_split("Volcanoes are interesting natural features", " ")
## [[1]]
## [1] "Volcanoes" "are" "interesting" "natural" "features"
Ignoring uppercase and lowercase letters
volcano %>%
filter(str_detect(
country,
regex("united states", ignore_case = TRUE)
))
## # A tibble: 99 × 35
## volcano_number...1 volcano_name primary_volcano_type last_eruption_year
## <dbl> <chr> <chr> <chr>
## 1 321040 Adams Stratovolcano 950
## 2 284160 Agrigan Stratovolcano 1917
## 3 284141 Ahyi Submarine 2014
## 4 311320 Akutan Stratovolcano 1992
## 5 311390 Amak Stratovolcano 1796
## 6 311190 Amukta Stratovolcano 1997
## 7 284200 Anatahan Stratovolcano 2008
## 8 312090 Aniakchak Caldera 1931
## 9 284150 Asuncion Stratovolcano 1906
## 10 311160 Atka Stratovolcano(es) 1812
## # ℹ 89 more rows
## # ℹ 31 more variables: country <chr>, region <chr>, subregion <chr>,
## # latitude <dbl>, longitude <dbl>, elevation <dbl>, tectonic_settings <chr>,
## # evidence_category <chr>, ...13 <chr>, major_rock_1 <chr>,
## # major_rock_2 <chr>, major_rock_3 <chr>, major_rock_4 <chr>,
## # major_rock_5 <chr>, ...19 <chr>, volcano_number...20 <chr>,
## # volcano_number...21 <chr>, volcano_number...22 <lgl>, …