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 / gist:9190970
Last active December 15, 2023 03:19
Categorical data analysis in R - a resource list
@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 / 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 / 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 / gist:7740998
Last active September 6, 2020 02:22
Random number generation in R (rstats, #rstats)

Random numbers in R

The creation of random numbers, or the random selection of elements in a set (or population), is an important part of statistics and data science. From simulating coin tosses to selecting potential respondents for a survey, we have a heavy reliance on random number generation.

R offers us a variety of solutions for random number generation; here's a quick overview of some of the options.

runif, rbinom, rnorm

One simple solution is to use the runif function, which generates a stated number of values between two end points (but not the end points themselves!) The function uses the continuous uniform distribution, meaning that every value between the two end points has an equal probability of being sampled.

@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_