Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Multi-tasking

Martin Monkman MonkmanMH

🎯
Multi-tasking
View GitHub Profile
@MonkmanMH
MonkmanMH / separate_lat_lon.R
Created Oct 22, 2021
use `separate()` function from {tidyr} to split a variable
View separate_lat_lon.R
# 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 Feb 12, 2021
dplyr's ungroup function
View ungroup_example.Rmd
# {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
View penguins_quantcut.R
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))
View rename_master_to_main.Rmd
---
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 Oct 6, 2020
install {annotater} package
View annotater_install.R
# 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")
View postal_code_loop.R
# 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 Oct 7, 2020
git command to rename branch of repo
View main_branch.md

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 Apr 19, 2020
geom_bar vs geom_col
View geom_bar_col.Rmd
---
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 Nov 4, 2019
Answer key for tidyverse transform data exercises
View transform_data_solutions.Rmd
---
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)
View goalsscored.R
### ---
#
# from @expersso
set.seed(894) # number of regular season NHL goals Wayne Gretzky scored
x <- replicate(10000, sum(sample(0:1, 20, TRUE, c(0.945, 0.055))))
table(ifelse(x == 0, "Team A win", ifelse(x == 1, "Draw", "Team B win"))) / 100