Skip to content

Instantly share code, notes, and snippets.

View jeremy-allen's full-sized avatar

Jeremy Allen jeremy-allen

View GitHub Profile

App Install Plan

Critical

@jeremy-allen
jeremy-allen / separate_and_long.R
Last active November 22, 2021 17:42
separate and pivot_longer with multiple obs as column names
library(tidyverse)
# This fake data matches real-world data I was given.
# The level of analysis is the units of peppers (rows),
# but we see subunits here, and need to make those the
# level of analysis - one subunit per row, i.e., tidy.
# Notice the description column is not named like the
# other subunit columns.
# This data matches the example described on the pivot_longer
# help page as "Multiple observations per row."
@jeremy-allen
jeremy-allen / write_csv_files.R
Created August 5, 2021 00:47
Create fake data and write 20,000 CSV file to disk
library(data.table) # for fwrite()
library(stringr)
library(purrr)
# file names we will use when writing 20,000 CSV files to disk
filenames <- paste0("files/", "a_",
str_pad(1:20000, width = 5,side = "left", pad = "0"),
".csv")
# some vectors we can sample from to make a table
@jeremy-allen
jeremy-allen / delete_tweets.R
Created May 11, 2021 13:09
Delete all tweets older than 30 days unless they have certain hashtags
library(here)
library(rtweet)
library(tidyverse)
library(assertthat)
#---- prep ----
my_dir <- here()
@jeremy-allen
jeremy-allen / order_months.R
Created May 12, 2020 15:47
return a character vector of the last 12 months, ending with current month
order_months <- function(x = NULL, label = "abb") {
# This function takes a given month number or the current month
# number and returns a character vector of the last 12 months,
# including current month. For example, if it is now February:
# "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" "Jan" "Feb"
# is returned with the current month at the end.
# This makes a nice x axis if you need to plot something for
# the "last 12 months".
# if x is NULL the current month is taken from Sys.Date
@jeremy-allen
jeremy-allen / find_index.R
Last active May 10, 2020 00:32
Get the position number of the first instance of a thing in one column, and use that number to pick something from another column
library(data.table)
library(dplyr)
library(purrr)
# lots of dates
date = seq.Date(from = as.Date("1900-01-01"),
to = as.Date("2900-12-31"),
by = "day")
# lots of cases