Skip to content

Instantly share code, notes, and snippets.

@aghaynes
aghaynes / git_remotes_sync.txt
Created December 7, 2018 10:00
use git remotes (e.g. to sync a fork back to it's original repo)
# add remote
git remote add <name> <url>
# fetch and merge
git fetch <name>
git merge <name>
# see also: https://help.github.com/articles/syncing-a-fork/
@aghaynes
aghaynes / lmer_vs_inla_lmm.R
Created November 22, 2018 16:35
Compare variances from lmer and INLA for a linear mixed model (random intercept)
#
# Compare lmer and inla for LMM
# largely taken from Spatial and spatio-temporal bayesian models with R-INLA (Blangiardo & Cameletti, 2015), section 5.4.2
#
m <- 10000 # N obs
set.seed(1234)
x <- rnorm(m)
group <- sample(seq(1, 100), size = m, replace = TRUE)
@aghaynes
aghaynes / biasfig.R
Last active January 8, 2019 13:06
REDCap metaanalysis data dictionary
# Download data
token <- readLines("PATH/TO/TOKEN")
library(httr)
url <- "PATH/TO/API"
# data itself
x <- POST(url,
config = httr::config(SSL_VERIFYPEER = FALSE),
body = list(token = token,
content = "record", type = "flat",
format = "csv"))
@aghaynes
aghaynes / bufferunion.r
Created June 11, 2018 12:00
Code to create a buffer around points, linking nearby points together via buffers
library(osmdata)
library(rgdal) ; library(maptools) ; library(rgeos)
q0 <- opq(bbox = "Bern, Switzerland", timeout = 60)
q1 <- add_osm_feature(q0, key = 'building', value = "hospital")
x <- osmdata_sp(q1)
library(leaflet)
@aghaynes
aghaynes / intersect_many_polygons.r
Created May 22, 2018 12:59
Toy example of a one to many intersection between points and polygons
library(sp)
# create polygons
p1 <- matrix(c(1,1,
2,1,
4,2,
3,2), ncol = 2, byrow = TRUE)
p2 <- matrix(c(2.2,1,
3,1,
3,2,
#==========================================================#
# Visualizing Facebook networks with friend photos #
# Katya Ognyanova, www.kateto.net, @ognyanova #
#==========================================================#
# Find more details at http://bit.ly/fbgephi
#==========================================================#
#### Collect the data using the RFacebook package ####
@aghaynes
aghaynes / passgen.r
Created June 3, 2016 10:22
Generate a random password using R
passgen <- function(n = 8){
chars <- c(LETTERS, letters, as.character(seq(0,9,1)), "!", "$", ".", "_", "-", "?")
paste0(sample(chars, n), collapse = "")
}
passgen()
@aghaynes
aghaynes / colsearch
Created March 2, 2015 10:24
Search all columns in a dataframe for a piece of text
colsearch <- function(data, query, ignore.case=TRUE){
n <- ncol(data)
c <- character(0)
for(i in 1:n){
if(TRUE %in% grepl(query, unique(data[,i]), ignore.case=ignore.case)) c <- c(c, names(data)[i])
}
c
}
@aghaynes
aghaynes / 0_reuse_code.js
Created August 5, 2014 13:06
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console