Skip to content

Instantly share code, notes, and snippets.

@bbolker
Last active July 26, 2025 20:50
Show Gist options
  • Save bbolker/2046e47505cccb6c12a3077638797376 to your computer and use it in GitHub Desktop.
Save bbolker/2046e47505cccb6c12a3077638797376 to your computer and use it in GitHub Desktop.
tabulate ending strings of chemical elements
## https://help.displayr.com/hc/en-us/articles/360003582875-How-to-Import-a-Wikipedia-Table-Using-R
library(rvest)
page = read_html("https://en.wikipedia.org/wiki/List_of_chemical_elements")
my.table = html_node(page, ".wikitable") |>
html_table(fill = TRUE)
nm_vec <- my.table[[3]][-1]
vowels <- c("a","e","i","o","u")
consonants <- setdiff(letters, vowels)
cc <- function(x) paste(x, collapse = "")
str <- sprintf("^.*[%s]([%s]+[%s]*)$", cc(consonants), cc(vowels), cc(consonants))
endings <- gsub(str, "\\1", nm_vec, ignore.case = TRUE)
tt <- table(endings)
endings[endings %in% names(tt[tt==1])] <- "other"
ss <- sort(table(endings), decreasing = TRUE)
paste(sprintf('"%s"=%d', names(ss), ss), collapse = ";") |> cat(quote=FALSE)
## endings
## ium other on e en um er
## 79 13 9 7 4 4 2
get_els <- function(s) {
nm_vec[which(substr(nm_vec, nchar(nm_vec)-(nchar(s)-1), nchar(nm_vec)) == s)]
}
get_els("er")
get_els("en")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment