Skip to content

Instantly share code, notes, and snippets.

@benmarwick
benmarwick / gist:e4fa3fdcbf328d6ef6e38f219e9f8a2a
Last active April 29, 2023 21:35
Plot time series of emails resulting from a search, in this case my emails about ARCHY 109 during the Fall quarter of 2018
# get the dates of all emails with a certain search string, and plot
# install.packages(c("gmailr", "lubridate", "tidyverse"))
suppressPackageStartupMessages(library(gmailr))
suppressPackageStartupMessages(library(tidyverse))
archy_109 <-
messages(
search = 'ARCHY 109',
num_results = 1000,
@benmarwick
benmarwick / BITSS-NTU-workshop.R
Created September 16, 2018 17:36
Code for workshop on R for archaeologists at the National Taiwan University as part of my BITSS series
# load the libraries
library(tidyverse)
library(readxl)
# load the data
pottery <-
read_excel("data/HTSKY-RIM.xlsx",
col_types = "text")
@benmarwick
benmarwick / giur-zones-with-ggplot2.R
Last active June 18, 2023 02:58
Geometric Index of Unifacial Retouch Zone visualisation with ggplot2
library(ggplot2)
# coords for vertices of polygons that make the zones
polydata <- rbind(
data.frame(x = c(0, 0.01, 0.99, 1),
y = c(0, 0.25, 0.25, 0),
group = "zone 1",
fill = "grey80", xc = .5, yc = 0.125),
data.frame(x = c(0, 0.025, 0.495, 0.495),
y = c(-0.01, -0.25, -0.25, -0.01),
@benmarwick
benmarwick / Writing-session-productivity.R
Created July 12, 2018 17:13
Writing session productivity
library("googlesheets")
suppressPackageStartupMessages(library("dplyr"))
third_party_gap <- "https://docs.google.com/spreadsheets/d/1xbMp7UZ7Nusjb2u1b1RihZrNzPMINZ8BHy9x2urGTzo/edit#gid=0" %>%
gs_url()
wp <-
third_party_gap %>%
gs_read(ws = "Writing progress")
@benmarwick
benmarwick / gdoc-revisions-analysis.R
Last active July 16, 2024 13:24
How to get access to specific revisions (and details about it) of a google drive document? cf. https://github.com/tidyverse/googledrive/issues/218
# related to my question here: https://github.com/tidyverse/googledrive/issues/218
# How to get access to specific revisions of a google drive document? Yes, we can do that.
# How to get the username for each revision, and the size of the document at each revision (bytes or words, assuming we are specifically talking about docs and not sheets or other types of files) Yes, we can do that.
# Goal is to measure the contributions of each author to a collaboratively-authored google doc with as little work as possible
# How to get access to specific revisions of a google drive document?
# Getting the revisions for a Google Doc
@benmarwick
benmarwick / cluster-images-by-hand-drawn-outlines
Last active August 4, 2018 17:01
Cluster images traced by hand, using EFA and PCA, and plot
# to make traces...
# Import an EPS file in Inkscape
# Trace over it to show half of vessel profile, from top of rim to centre of base, about 20-30 segements
# detelete EPS from from Inkscape
# export PNG of trace from Inkscape
library(magick)
lf <- list.files("../traces", full.names = TRUE)
@benmarwick
benmarwick / cluster-images.R
Created May 19, 2018 03:54
Cluster images by similarity, and plot
# read in the images...
library(magick)
img_paths <- list.files("../outline_P/", full.names = TRUE )
n <- 100 # just get a sample
imgs <- image_read(img_paths[1:n])
# reduce size
imgs_cropped <- image_crop(imgs, "100x100")
@benmarwick
benmarwick / cp-analysis.R
Last active May 10, 2018 00:08
sketch for ffsg change point analysis
# Change point analysis
library(tidyverse)
# get the data of population per state and fatalities per state
source(here::here("/Analysis/Tables/permillcalculation.R"))
# compute fatalities per 1000 peple per year per state, using
# the custom function in permillcalculation.R
ratio_data <- permillcalc()
@benmarwick
benmarwick / simulate-Wright-Fisher-Model-of-selection-and-random-genetic-drift.R
Last active November 22, 2024 12:58
Simulates the Wright-Fisher Model of selection and random genetic drift
# data.frame to be filled
wf_df <- data.frame()
# effective population sizes
sizes <- c(5, 10, 100, 500)
# starting allele frequencies
starting_p <- c(.01, .1, .5, .8)
# number of generations
@benmarwick
benmarwick / gist:70f92dd61700abab1b590afa0040e3fa
Created April 27, 2018 22:17
using sf for points in polygon spatial join
library(sf)
library(tidyverse)
# read in the shapefile first, that gives us the CRS for the analysis
polygons <- st_read("polygons.shp")
# read in the points
points <- read_csv('points.csv')