Skip to content

Instantly share code, notes, and snippets.

View geotheory's full-sized avatar

Robin Edwards geotheory

View GitHub Profile
# 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') |>
{
"id": "0igifmdu25",
"product_class": "",
"process": "",
"source_type": "",
"product_category": "BIOGRAPHY",
"regions": [],
"primary_country_tag": "ZMB",
"topic_tags": [],
"country_tags_full": ["Zambia"],
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
}
{
"responses": [
{
"faceAnnotations": [
{
"boundingPoly": {
"vertices": [
{
"x": 195,
"y": 130
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
# 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))
# string markdown to html conversion (HT https://github.com/matt-dray)
purrr::map_chr(
c("# title", "regular **bold**"),
~ markdown::renderMarkdown(text = .x)
)
#> [1] "<h1>title</h1>\n"
#> [2] "<p>regular <strong>bold</strong></p>\n"
library(tidyverse)
set.seed(7)
N = 1000
x = rnorm(N)
p = plogis(0.2*x - 0.8)
y = rbinom(N, 1, p)
tibble(x, y) %>%
mutate(z = cut_number(x, 5)) %>%
// debug javscript errors with alerts
window.onerror = function (msg, url, lineNo, columnNo, error) {
var error_msg = ['msg: ' + msg, 'url: ' + url, 'lineNo: ' + lineNo, 'columnNo: ' + columnNo, 'error: ' + error].join('\n');
window.alert(error_msg);
}