Skip to content

Instantly share code, notes, and snippets.

Avatar

Robin Edwards geotheory

View GitHub Profile
View fill-missing-timeseries.R
# identify and fill missing data points in timeseries data with regular-periodicity
require(padr)
#> Loading required package: padr
df <- data.frame(date = as.Date('2016-04-01') + 3*c(0,1,3,5,6), val = sample(5)) |>
print()
#> date val
#> 1 2016-04-01 3
#> 2 2016-04-04 1
View wayback-engine.R
require(wayback)
url = 'https://bbc.com'
wb = wayback::archive_available(url)
wb$closet_url
wb_mems = wayback::get_mementos(u)
wb_mems$link
View %IN%.R
# String in text (vectorised)
# A Python-style String in Text operator that ignores case. Either x or pattern must be a single value. Returns a logical value or vector.
{\(pattern, x) stringi::stri_detect_regex(x, pattern, case_insensitive = TRUE)} -> `%IN%`
# usage
'dog' %IN% 'the nice dog said hello'
#> [1] TRUE
View list-dependencies.R
# Function to report all explicit R dependencies in current directory (recursive)
# This ignores dependencies of dependencies
report_dependencies = function(path = './', print = FALSE, simple = TRUE){
require(stringr)
files = list.files(path = path, pattern = '.R$', recursive = TRUE, full.names = TRUE)
scripts = lapply(files, readLines, warn = FALSE) |> unlist() |> paste(collapse = '\n')
loaded = scripts |> str_extract_all('(?<=(library)|(require)[(])[a-zA-Z0-9_.]+') |> unlist() |>
str_remove_all('["\']') |> str_remove(',.*') |> unique() |> sort()
View case_lump.R
# replace strings with mode case
require(dplyr)
case_lump = function(x){
x1 = tibble::enframe(x, name=NULL) |> mutate(id = 1:n(), lower = tolower(value))
x1 |> count(value, lower, sort = TRUE) |>
group_by(lower) |> slice(1) |> ungroup() |>
rename(mode = value) |>
left_join(x1, by = 'lower') |>
View bbcm-schema.json
{
"id": "0igifmdu25",
"product_class": "",
"process": "",
"source_type": "",
"product_category": "BIOGRAPHY",
"regions": [],
"primary_country_tag": "ZMB",
"topic_tags": [],
"country_tags_full": ["Zambia"],
View html_text_parent.R
require(rvest)
html_text_parent = function(x){
x2 = rlang::duplicate(x, shallow = FALSE)
children = html_children(x2)
xml2::xml_remove(children)
txt = html_text(x2)
x = map(children, ~ xml2::xml_add_child(x, .value = .x))
txt
}
View gdelt-vgkg-example-codings.json
{
"responses": [
{
"faceAnnotations": [
{
"boundingPoly": {
"vertices": [
{
"x": 195,
"y": 130
View dogs-on-the-titanic.csv
breed name owner ticket cabin kenneled survived
King Charles Spaniel NA William Ernest Carter 113760 B96 B98 TRUE FALSE
Airedale Terrier NA William Ernest Carter 113760 B96 B98 TRUE FALSE
Chow Chow Chow-Chow Harry Anderson 19952 E12 TRUE FALSE
French Bulldog Gamin de Pycombe Robert Williams Daniel 113804 A TRUE FALSE
Airedale Terrier Kitty John Jacob Astor 11754 C62 TRUE FALSE
Pomeranian Bebe/Lady Margaret Bechstein Hays 11767 C54 FALSE TRUE
Pomeranian NA Elizabeth Jane Anne Rothschild 17603 C FALSE TRUE
Pekingese Sun Yat Sen Henry Sleeper Harper & Myra Raymond Harper 17572 D33 FALSE TRUE
toy dog Freu Freu Helen Margaret Bishop 11967 B49 FALSE FALSE
View join-overwrite.R
# remotes::install_github("moodymudskipper/safejoin")
require(safejoin)
require(tidyverse)
#> Loading required package: tidyverse
d1 = tibble(id = c(1,2,3), x = c('A','B','C'), y = c(5,7,9))
d2 = tibble(id = c(2,3,4), x = c('B','CC','D'), y = c(7,9,11))
safe_full_join(d1, d2, by = 'id', conflict = ~ coalesce(.y, .x))