Skip to content

Instantly share code, notes, and snippets.

#'% Using Dates and Times in R
#'% Bonnie Dixon
#'% 14-02-10 15:09:57
#'
#' *Today at the [Davis R Users'
#' Group](http://www.noamross.net/davis-r-users-group.html), [Bonnie
#' Dixon](http://ffhi.ucdavis.edu/people/directory/bmdixon) gave a tutorial on the
#' various ways to handle dates and times in R. Bonnie provided this great script
#' which walks through essential classes, functions, and packages. Here it is piped through
#' `knitr::spin`. The original R script can be found as a gist
@benjamin-chan
benjamin-chan / testParallel.md
Last active August 29, 2015 14:05
Test parallelization

Test parallel

require(MASS)
## Loading required package: MASS
@benjamin-chan
benjamin-chan / harmonograph.r
Created October 13, 2014 23:03
harmonograph.r
# http://aschinchon.wordpress.com/2014/10/13/beautiful-curves-the-harmonograph/
f1 <- jitter(sample(c(2, 3), 1))
f2 <- jitter(sample(c(2, 3), 1))
f3 <- jitter(sample(c(2, 3), 1))
f4 <- jitter(sample(c(2, 3), 1))
d1 <- runif(1, 0, 1e-02)
d2 <- runif(1, 0, 1e-02)
d3 <- runif(1, 0, 1e-02)
d4 <- runif(1, 0, 1e-02)
@benjamin-chan
benjamin-chan / icd9.r
Last active August 29, 2015 14:08
ICD-9 code lookup
# Read ICD-9 diagnosis and procedure code descriptions
descLength <- "LONG" # Change "SHORT" to "LONG" for more detailed description
# Use Version 32 from cms.gov
# Version 32 Full and Abbreviated Code Titles – Effective October 1, 2014
# Update if necessary
url <- "http://www.cms.gov/Medicare/Coding/ICD9ProviderDiagnosticCodes/Downloads/ICD-9-CM-v32-master-descriptions.zip"
@benjamin-chan
benjamin-chan / readRUCA.r
Last active August 29, 2015 14:08
Oregon Office of Rural Health urban/rural designation
url <- "http://www.ohsu.edu/xd/outreach/oregon-rural-health/data/rural-definitions/upload/OR-Zip-Codes-Urban-Rural-Designations.xlsx"
f <- tempfile()
download.file(url, f, mode="wb")
if (!require(xlsx)) {
repos <- "http://cran.us.r-project.org"
install.packages("xlsx", dependencies=TRUE, repos=repos)
rm(repos)
}
require(xlsx)
ruca <- read.xlsx(f, 1)
@benjamin-chan
benjamin-chan / callSAS.r
Last active October 12, 2015 17:01
R script to call SAS
callSAS <- function (f, path=tempdir(), workDir=sprintf("E:\\Share\\Temp\\%s\\SASWork", Sys.info()["user"])) {
# Usage:
# > callSAS("getClaimsMed.sas")
if (grepl(".sas$", f)) {
exeFile <- "C:\\Program Files\\SASHome\\SASFoundation\\9.3\\sas.exe"
sasFile <- sprintf("%s\\%s", path, f)
logFile <- sprintf("%s\\%s", path, gsub(".sas$", ".log", f))
cmd <- sprintf("\"%s\" -sysin \"%s\" -log \"%s\" -print \"%s\" -work \"%s\"",
exeFile,
sasFile,
@benjamin-chan
benjamin-chan / callStata.r
Last active August 29, 2015 14:14
Call Stata .do batch file from R
callStata <- function (f, path=tempdir(), tempPath="E:\\Share\\Temp") {
# Usage:
# > callStata(do-file-name-as-character-object,
# path=path-of-do-file-as-character-object,
# tempPath=temp-folder-path-as-character-object)
#
# A log file is created in the same folder as the .do file
#
# Because Stata is stupid, dynamically create a temporary batch file.
# The batch file contains 3 lines
@benjamin-chan
benjamin-chan / readZipCodeDatabase.r
Last active August 29, 2015 14:14
Read zip code database
repos <- "http://cran.us.r-project.org"
if (!require(data.table)) {install.packages("data.table", dependencies=TRUE, repos=repos)}
require(data.table)
url <- "http://www.unitedstateszipcodes.org/zip_code_database.csv"
zip_code_database <- fread(url,
colClasses=c(rep("character", 9),
rep("numeric", 2),
rep("character", 2),
rep("integer", 2),
rep("character", 1)),
@benjamin-chan
benjamin-chan / sessionInfo.R
Last active November 19, 2015 05:28
Session information code chunk for R
# .timeStart <- Sys.time() # Defined at start of Rmd
list(timeStart = ifelse(exists(".timeStart"), format(.timeStart), NA),
timeEnd = Sys.time(),
timeElapsed = ifelse(exists(".timeStart"),
format(Sys.time() - .timeStart, format=difftime),
NA),
Sys.info = Sys.info(),
sessionInfo = sessionInfo())
# History files
.Rhistory
# Example code in package build process
*-Ex.R
# R data files from past sessions
.Rdata
# RStudio files