Skip to content

Instantly share code, notes, and snippets.

@ajdamico
ajdamico / nona.R
Created June 28, 2012 01:22
binary operators that never return missing values - how to remove NA values from logical tests
#create the remove NA function
no.na <-
function( x , value = FALSE ){
x[ is.na( x ) ] <- value
x
}
@ajdamico
ajdamico / rare events in survey sampling.R
Last active November 29, 2015 12:03
survey research does a lousy job of estimating prevalence rates for unlikely events
xl <- seq( 0 , 10000 , 100 )
my_title <- 'if we randomly choose X people from some population and determine that none have a disease,\nwe can be 95% confident that the population prevalence rate is below Y% for this particular affliction.'
plot( xl , qbeta(1 - 0.05, 1 , xl ) , ylim=c(0,.01),axes=F,xlab="",ylab="",main="\nhow rare is a rare disease?\n\nsurvey research does a lousy job of estimating prevalence rates for unlikely events")
axis(2, at=seq(0,.01,.001), lab=paste0(seq(0,.01,.001)*100,"%"), las=TRUE)
axis(1, at=seq(0,10000,1000), lab=prettyNum(seq(0,10000,1000),big.mark=","), las=TRUE)
text( 3500 , 0.0075 , label = my_title , cex = 1.5, adj=c(0,0))
z <- data.frame( x = 0:100000 , y = qbeta(1 - 0.05, 1 , 0:100000 ) )
@ajdamico
ajdamico / puma of my childhood.R
Created January 20, 2013 03:28
the puma of my childhood
# install mapping packages
install.packages( c( 'sp' , 'rgdal' ) )
# load necessary libraries
require(sp)
require(rgdal)
# create a temporary file and directory
tf <- tempfile() ; td <- tempdir()
@ajdamico
ajdamico / La_rs_bughunt.R
Created February 22, 2016 20:34
La_rs bughunt
# # # # # # # # # # # # # # # # #
# # set the working directory # #
# # # # # # # # # # # # # # # # #
# setwd( "C:/My Directory/SWMAP/" )
# # # # # # # # # # # # # # # #
# # example survey data set # #
# # # # # # # # # # # # # # # #
library(downloader)
# install.packages( c("MonetDB.R", "MonetDBLite" , "survey" , "SAScii" , "descr" , "downloader" , "digest" , "stringr" , "R.utils" , "RCurl" ) , repos=c("http://dev.monetdb.org/Assets/R/", "http://cran.rstudio.com/"))
library(SAScii) # load the SAScii package (imports ascii data with a SAS script)
library(RCurl) # load RCurl package (downloads https files)
library(stringr) # load stringr package (manipulates character strings easily)
library(downloader) # downloads and then runs the source() function on scripts from github
library(MonetDB.R) # load the MonetDB.R package (connects r to a monet database)
@ajdamico
ajdamico / top 25 words weighted by download.R
Created March 11, 2018 18:08
twenty-five most common words in CRAN title + description fields, weighted by downloads
# devtools::install_github( "ajdamico/lodown" )
library(tm)
library(tidyverse)
library(rvest)
cranlogs_html <- read_html( "http://cran-logs.rstudio.com/" )
gz_files <- html_attr( html_nodes( cranlogs_html , "a" ) , "href" )
x <-
structure(list(name = c("George Washington", "John Adams", "Thomas Jefferson",
"James Madison", "James Monroe", "John Quincy Adams", "Andrew Jackson",
"Martin Van Buren", "William Henry Harrison", "John Tyler", "James K. Polk",
"Zachary Taylor", "Millard Fillmore", "Franklin Pierce", "James Buchanan",
"Abraham Lincoln", "Andrew Johnson", "Ulysses S. Grant", "Rutherford B. Hayes",
"James A. Garfield", "Chester Arthur", "Grover Cleveland", "Benjamin Harrison",
"William McKinley", "Theodore Roosevelt", "William Howard Taft",
"Woodrow Wilson", "Warren G. Harding", "Calvin Coolidge", "Herbert Hoover",
library(survey)
library(lodown)
cpsasec_cat <- get_catalog( "cpsasec" , output_dir = tempdir() )
lodown( "cpsasec" , subset( cpsasec_cat , year == 2017 ) )
plot(
c( 15 , 75 ) ,
library(rvest)
tf <- tempfile()
tf2 <- tempfile()
# download and unzip
download.file( "https://dumps.wikimedia.org/other/pagecounts-ez/merged/2018/2018-05/pagecounts-2018-05-27.bz2" , tf , mode = 'wb' )
R.utils::bunzip2( tf , tf2 , remove = FALSE )
# import and restrict to english
@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 )