Skip to content

Instantly share code, notes, and snippets.

View MonkmanMH's full-sized avatar
🎯
Multi-tasking

Martin Monkman MonkmanMH

🎯
Multi-tasking
View GitHub Profile
@MonkmanMH
MonkmanMH / breaks_ts_min_max.R
Last active September 29, 2022 17:01
Y axis breaks at min & max value {ggplot2}
# modified from / inspired by @GShotwell
# gist: https://gist.github.com/GShotwell/b19ef520b6d56f61a830fabb3454965b
df <- tibble(
value = rnorm(100, 50, 10),
date = seq.Date(from = ymd("2022-01-01"),
ymd("2022-04-10"),
by = "day"))
@MonkmanMH
MonkmanMH / separate_lat_lon.R
Created October 22, 2021 03:00
use `separate()` function from {tidyr} to split a variable
# OBJECTIVE:
# * latitude and longitude stored with degrees with degree symbol and minutes with decimal
# * separate into two columns for each, with degrees and minutes separated
# step 1 - create a test tibble called "geo_loc"
geo_loc <- tribble( ~ latitude, ~ longitude,
"48º 4.206", "124º 45.553",
"46º 59.4942", "124º 12.6362")
# ---
# step 2 - split into degrees and minutes columns
@MonkmanMH
MonkmanMH / ungroup_example.Rmd
Last active February 12, 2021 01:45
dplyr's ungroup function
# {dplyr}'s `ungroup()` function
```{r}
# packages
library(gapminder)
library(dplyr)
```
In this example, we calculate the difference in a country's life expectancy from the continent's mean life expectancy
- for example, the difference between life expectancy in Canada and the mean life expectancy of countries in the Americas
penguins <- palmerpenguins::penguins
penguins %>%
filter(!is.na(sex)) %>%
mutate(bm_quart = gtools::quantcut(body_mass_g, q=4))
penguins_2 <- penguins %>%
filter(!is.na(sex)) %>%
select(species, sex, body_mass_g) %>%
# mutate(bm_quart = gtools::quantcut(body_mass_g, q=4))
@MonkmanMH
MonkmanMH / rename_master_to_main.Rmd
Created October 7, 2020 14:24
Rename GitHub repos
---
title: "rename github repo"
author: "Martin Monkman"
date: "2020/10/05"
output: html_document
---
From
https://www.r-bloggers.com/2020/07/5-steps-to-change-github-default-branch-from-master-to-main/
@MonkmanMH
MonkmanMH / annotater_install.R
Created October 6, 2020 23:34
install {annotater} package
# short script to install the {annotater} package
# reference: https://github.com/luisDVA/annotater
# Step 1: install the {remotes} package
install.packages("remotes")
# Step 2: install {annotater} from the GitHub source
remotes::install_github("luisDVA/annotater")
@MonkmanMH
MonkmanMH / postal_code_loop.R
Last active July 22, 2020 23:50
postal code loop
# dplyr::case_when to find and clean FSA
#
# Notes:
# * FSA = "Forward Sortation Area" in Canadian postal parlance
# * the regex finds British Columbia FSAs (starting with "V")
FSA_list <- df %>%
mutate(FSA_clean = case_when(
str_detect(FSA, "V\\d.$") == TRUE ~ FSA,
TRUE ~ NA_character_
@MonkmanMH
MonkmanMH / main_branch.md
Last active October 7, 2020 14:24
git command to rename branch of repo

rename from "master" to "main"

git branch -m master main

push to github

git push -u origin main

and delete the original

git push origin --delete master

But...you can do this as an Rmd with the git commands in a bash chunk

@MonkmanMH
MonkmanMH / geom_bar_col.Rmd
Created April 19, 2020 15:55
geom_bar vs geom_col
---
title: "geom_col vs geom_bar"
author: "Martin Monkman"
date: "2020/04/19"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
@MonkmanMH
MonkmanMH / transform_data_solutions.Rmd
Created November 4, 2019 03:34
Answer key for tidyverse transform data exercises
---
title: "Transform Data"
subtitle: "hands-on examples, with answers"
output: html_notebook
---
<!-- This file by Charlotte Wickham (with some modifications by Martin Monkman) is licensed under a Creative Commons Attribution 4.0 International License, adapted from the orignal work at https://github.com/rstudio/master-the-tidyverse by RStudio and https://github.com/cwickham/data-science-in-tidyverse-solutions. -->
```{r setup}
library(tidyverse)