Skip to content

Instantly share code, notes, and snippets.

@ajdamico
ajdamico / import SAS ASCII file into R with SAS INPUT text file
Created May 4, 2010 22:11
import SAS ASCII file into R with SAS INPUT text file
##select file containing SAS input procedure
SASinput <- readLines(file.choose())
##find the first INPUT line
firstline<-min(grep("INPUT @1",toupper(SASinput)))
##find the first semicolon ending that input line
a<-grep(";",toupper(SASinput))
lastline<-min(a[a>firstline])
@ajdamico
ajdamico / calculate your ten thousandth day on earth with R
Created July 24, 2010 18:26
calculate your ten thousandth day on earth with R
#replace my birthday with yours
birthday <- strptime(c("8.18.1982"), format = "%m.%d.%Y", tz="EST")
#r counts everything in seconds, so this code multiplies by 60 seconds in a minute, 60 minutes in an hour, 24 hours in a day.
days <- function(u) {
x <- u * 60 * 60 * 24
return(x)
}
#change these numbers to whichever days you'd like to see, separated by commas
@ajdamico
ajdamico / download the medical expenditure panel survey.R
Created November 9, 2010 19:36
download all publicly-available data files for every year of the Medical Expenditure Panel Survey
library(RCurl)
setwd("R:\\Medical Expenditure Panel Survey\\Data")
#input all available MEPS public use file numbers
year <- c(1996:2008)
consolidated <- c(12,20,28,38,50,60,70,79,89,97,105,113,NA)
conditions <- c("06r",18,27,37,52,61,69,78,87,96,104,112,NA)
jobs <- c("07",19,25,32,40,56,63,74,83,91,100,108,116)
prpf <- c(24,47,47,47,47,57,66,76,88,95,103,111,119)
@ajdamico
ajdamico / concentration of health expenditures.R
Created November 10, 2010 20:52
draw a curve showing the concentration of all healthcare expenditures in the united states
#warning: excludes the institutionalized population!
#install survey and foreign packages
#-- only run these lines of code the first time
#install.packages("foreign")
#install.packages("survey")
#designate each of the MEPS consolidated files -
consolidated_files <- c( 12 , 20 , 28 , 38 , 50 , 60 , 70 , 79 , 89 , 97 , 105 , 113 )
@ajdamico
ajdamico / multicore win64.R
Created December 10, 2010 21:58
run multicore processing in the windows 64 bit version of R
library(snowfall)
bigProcess <- function( x ) {
mean( replicate ( 10^6 , max( rnorm( 100 , x ) ) ) )
}
#run the function twice in parallel
start <- Sys.time()
sfInit(cpus=2,type='SOCK',parallel=TRUE)
sfSapply( c(1,5) , bigProcess )
@ajdamico
ajdamico / furman intro to r lecture.R
Created April 1, 2011 15:38
introduction to r lecture notes - furman center for real estate and urban policy
#three reasons to use R-
#it's free
#it's open source- package system
#it's a programming language for statistics.
x <- 1:5
@ajdamico
ajdamico / determine what pecent of fractions are non-repeating.R
Created August 7, 2011 08:25
calculate the percent of fractions that are repeating vs. non-repeating with R
# calculate the percent of fractions that are repeating vs. non-repeating
# install stringr if you don't already have it
#install.packages("stringr")
library( stringr )
options( digits=22 )
# this program looks at..
# 1/1, 1/2, 1/3, 1/4.. up to 1/x
@ajdamico
ajdamico / analyzing big survey data with limited computing resources.R
Created August 18, 2011 11:05
Convert large government survey data files into a SQLite database and then produce the principle set of statistical estimates and accompanying error terms, with limited RAM.
########################################
###read large csv into SQL DB
########################################
#set to the number of GB of RAM on computer
gbram <- 0.5
#set to CSV file directory
setwd("C:\\American Community Survey\\2009\\")
@ajdamico
ajdamico / download all files from an FTP site.R
Created September 19, 2011 22:45
download fifty years of National Health Interview Survey documentation PDFs
#install RCurl on your version of R if you don't already have it
#just run this once
#install.packages("RCurl")
#program start
#load the RCurl package
library(RCurl)
#set your output folder - this is where the pdfs will get saved
setwd("R:/National Health Interview Survey/documentation")
@ajdamico
ajdamico / NHIS download matrix.R
Created September 30, 2011 14:38
download every file for every year of the National Health Interview Survey and convert them all to csv and stata files
#load necessary libraries
library(stringr)
library(foreign)
library(survey)
library(RCurl)
#set the temporary directory to download all files to!
setwd("s:/temp")