Skip to content

Instantly share code, notes, and snippets.

@johnmackintosh
johnmackintosh / update-local-git.txt
Created March 14, 2023 11:33
code to update local repo from master to main
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
@johnmackintosh
johnmackintosh / rdsdl.R
Created January 13, 2023 12:07
download RDS file direct from github repository
path <- "https://github.com/owner/repo/raw/main/somedata.rds"
rdsdl <- function(path){
readRDS(url(path, "rb"))
}
@johnmackintosh
johnmackintosh / tidy-gather-pivot_longer-comparison.R
Created December 2, 2022 15:27
remember when tidyr::gather was a piece of cake?
### old way
```r
df %>%
mutate(row = row_number()) %>%
gather('column', 'source', -row, -N) # key = column, value = source, retain row and N
# further transforms
```
@johnmackintosh
johnmackintosh / create_folders.R
Last active October 25, 2022 12:53
quickly create subdiretories for RMarkdown outputs
# I want 18 separate folders for graphic outputs, and 17 separate folders for different outputs
# they will be saved in the "output" folder under my main project directory
library(fs)
library(here)
here <- here::here()
tablenos <- paste0("table",seq(1:17))
fignos <- paste0("fig",seq(1:18))
@johnmackintosh
johnmackintosh / test_frame.csv
Created September 25, 2022 22:32
data for NHSR caseload demo and answer
client_id referral_id team_desc referral_date discharge_date unique_id
1 1 Apple 2018-11-07T00:00:00Z 2019-10-29T00:00:00Z 1_1
2 1 Banana 2018-03-30T00:00:00Z 2_1
2 2 Apple 2020-03-14T00:00:00Z 2021-02-12T00:00:00Z 2_2
3 1 Banana 2017-01-18T00:00:00Z 2017-07-30T00:00:00Z 3_1
4 1 Apple 2020-09-02T00:00:00Z 2021-02-09T00:00:00Z 4_1
4 2 Apple 2017-06-20T00:00:00Z 2017-11-27T00:00:00Z 4_2
4 3 Clementine 2017-08-14T00:00:00Z 4_3
4 4 Clementine 2019-04-13T00:00:00Z 4_4
5 1 Banana 2019-11-21T00:00:00Z 2020-05-09T00:00:00Z 5_1
@johnmackintosh
johnmackintosh / caseload.R
Created September 25, 2022 22:22
as per Chris Beeley caseload question from NHSR demo and how-to
teams <- c("Apple", "Banana", "Clementine")
dates <- seq(as.Date("2017-01-01"), as.Date("2021-01-01"), "days")
# 5 columns- client id, referral id, team_desc, referral_date, discharge_date
test_frame <- purrr::map_dfr(1 : 100, function(x){
rnum <- sample(1 : 5, 1)
team_name <- sample(teams, rnum, replace = TRUE)
@johnmackintosh
johnmackintosh / update_rdatatable_with_lapply.R
Created August 30, 2022 08:43
how to use lapply and .SD to update columns within rdatatable
# columns to update:
char_cols <- c("areaname", "parent_area")
# 1. convert to character, then
# 2. replace '&' with 'and'
DT[,(char_cols) := lapply(.SD, as.character), .SDcols = char_cols]
DT[,(char_cols) := lapply(.SD, gsub, pattern = ' & ', replacement = ' and '), .SDcols = char_cols]
@johnmackintosh
johnmackintosh / nhsr-interval-counter.R
Last active June 21, 2022 21:26
answer to question on NHSR slack
library(tidyverse)
library(lubridate)
## each id is a person, and each row is one episode
## the two episode for id 1 overlap!
example_dates <- tibble(
id = c(1,1,2,2),
date_start = ymd(c("2020-01-01", "2020-01-03","2020-04-01", "2020-04-15")),
date_end = ymd(c("2020-01-05", "2020-01-10", "2020-04-04", "2020-04-16"))
) #%>%
@johnmackintosh
johnmackintosh / scotland-cusums.R
Created December 15, 2021 22:43
rolling 28 day positive case counts in Scotland council areas using PHS open data and {cusumcharter}
library(dplyr)
library(data.table)
library(ggplot2)
library(cusumcharter)
library(purrr)
library(tidyr)
library(ggExtra)
# make the link dynamic