Skip to content

Instantly share code, notes, and snippets.

@mjrich
mjrich / survey_data_weighting.R
Last active September 6, 2015 18:30
This is an example script for how to compute weights in base R using demographic variables like age & gender and then how to calculate frequencies while applying those weights
#
#You have a dataframe "df", with variables gender and age, and other variables of interest about radio listenership
#
#Add weights below. The population parameter over the prop.table(..) portion calculating sample statistics.
weight_male = .49500764/(prop.table(table(df$Gender))["Male"])
weight_male
weight_female = .50499236/(prop.table(table(df$Gender))["Female"])
weight_female
@mjrich
mjrich / spss_data_load.R
Last active December 26, 2015 03:49
script for opening SPSS data (from Afrobarometer) in R
library(foreign)
library(memisc)
library(Hmisc)
setwd("C:\\your\\working\\folder")
#Open with standard foreign package
data_frame_attempt1 <- read.spss("yourdata.sav")
#Open with memisc package HT http://stackoverflow.com/questions/3136293/read-spss-file-into-r
@mjrich
mjrich / Display_LatLon_OSM.R
Created August 7, 2013 19:28
R (markdown) code to overlay lat/longs on a static map with OSM base.
library("ggmap")
library("mapproj")
### Tunisia Map
``` {r Map, dpi=100,echo=FALSE, message=FALSE, comment=NA, fig.width=6}
# get map image from google
map <- get_map(location = c(lon = 9.489765, lat = 35.584840), source = "osm", zoom = 6)
@mjrich
mjrich / CrossTableCP.R
Created June 17, 2013 18:32
CrossTable {gmodels} hack so that output is comma separated for easy copy and paste into tabular spreadsheet software i.e. Excel, Open Office
#Unofficial Revision 2013/06/17
# Stripped-out whitespace and pipe delimeters and added comma separation for easier copying and pasting to spreadsheet software.
# Revision 2.2 2006/05/02
# Fix a bug when a matrix is passed as the 'x' argument
# Reported by Prof. Albert Sorribas same day
# Fix involved creating default values for RowData and ColData
# when there are no dimnames for the matrix
# Revision 2.1 2005/06/26
@mjrich
mjrich / gist:4946970
Last active December 13, 2015 17:18
shell script to back-up directory as tar
tar -cjvf BACKUP_file.tar.bz2 ./public_html #back-up website
tar -xjf archivefile.tar.bz2 #uncompress file
@mjrich
mjrich / README.md
Created November 6, 2012 04:48 — forked from nikhilrajendra/README.md
Thanking friends who wishes for your birthday
@mjrich
mjrich / google-refine-geocoding
Created July 15, 2012 21:13
Google Refine Geocoding
Step One
Fetch URL based on column:
'http://www.datasciencetoolkit.org/maps/api/geocode/json?sensor=false&address=' + escape(value, 'url')'
Step Two
Parse Json for longitude:
value.parseJson()["results"][0]["geometry"]["location"]["lng"]
@mjrich
mjrich / carto-choropleth
Created July 15, 2012 21:05
Choropleth map styling code in Carto
#nbhdata {
line-color:#594;
line-width:0.5;
polygon-opacity:1; }
#nbhdata[R_HomeOwn >= 0 ] { polygon-fill: #f0ffe1; }
#nbhdata[R_HomeOwn >= 9] { polygon-fill: #d9fcb9; }
#nbhdata[R_HomeOwn >= 18] { polygon-fill: #bbef8e; }
#nbhdata[R_HomeOwn >= 27] { polygon-fill: #9ad363; }
#nbhdata[R_HomeOwn >= 36] { polygon-fill: #6e843f; }