Skip to content

Instantly share code, notes, and snippets.

View mbacou's full-sized avatar

Mel B. mbacou

View GitHub Profile
@mbacou
mbacou / carto.css
Created November 27, 2013 22:46
Discrete Raster Data
@blank: transparent;
@snow: #ffffff;
@unused: #9a9a9a;
@urban: #e2bf58;
@agriculture: #91a487;
@grass: #6b7e60;
@forest: #46533f;
@water: #37526d;
Map { background-color:@water;}
@mbacou
mbacou / spamToNetCDF.R
Last active December 27, 2015 15:39
Convert SPAM 2000 to NetCDF
## ===================================================================================
## Title: Converting SPAM 2000 from CSV to NetCDF Format
## Project: SPAM
## Author: Bacou, Melanie <[email protected]>
## Date: April 11, 2013
## ===================================================================================
library(ncdf)
library(reshape2)
library(data.table)
@mbacou
mbacou / snippets.R
Last active June 12, 2017 22:16
Spatial operations: dissolve and overlay
setwd("/home/projects")
library(data.table)
library(rgdal)
library(rgeos)
library(maptools)
library(stringr)
# Load GAUL 2008 (release 2009) adm-2 from Darby
g <- readOGR("./maps/admin", "g2008_2")
@mbacou
mbacou / snippets.R
Last active November 5, 2024 10:51
How to read and write STATA variable and value labels from R.
# Using read.dta() to load a sample STATA data file
dta <- read.dta("my_toy_data.dta")
# Let's describe dta
str(dta)
#'data.frame': 95202 obs. of 13 variables:
# $ hh : num 1.01e+09 1.01e+09 1.01e+09 1.01e+09 1.01e+09 ...
#$ h14aq2 : Factor w/ 61 levels "matooke","matooke",..: 50 51 34 47 57 41 22 1 13 53 ...
#$ h14aq3 : Factor w/ 93 levels "Kilogram (kg)",..: 51 26 85 1 NA 1 55 70 1 33 ...
#$ h14aq4 : num 0.5 4 4 1 NA 1.5 5 3 4 10 ...
@mbacou
mbacou / snippets.el
Created July 28, 2013 14:08
publish-org customizations
'(org-export-html-preamble-format (quote (("en" "<script type=\"text/javascript\">
function rpl(expr,a,b) {
var i=0
while (i!=-1) {
i=expr.indexOf(a,i);
if (i>=0) {
expr=expr.substring(0,i)+b+expr.substring(i+a.length);
i+=b.length;
}
}
@mbacou
mbacou / snippets.R
Created July 12, 2013 11:58
Connecting to PostGIS
require(rgdal)
dsn="PG:dbname='gis'"
# What tables are in that database?
ogrListLayers(dsn)
# [1] "ccsm_polygons" "nongp" "WrldTZA"
# [4] "nongpritalin" "ritalinmerge" "metforminmergev"
# Get one:
polys = readOGR(dsn="PG:dbname='gis'","ccsm_polygons")
@mbacou
mbacou / snippets.R
Last active March 20, 2022 23:46
Working with attributes inside SpatialPolygonsDataFrame objects
## Example procedure for working with data (attributes) inside SpatialPolygonsDataFrame objects
# Load my favorite libraries for that sort of work
library(data.table)
library(rgdal)
# Load 2 shapefiles that, say, we want to merge.
# Note that you can read pretty much any spatial format using readOGR().
tza.l2 <- readOGR("./analysis/TZA-AC-07/maps", "tza-ac-07_L2")
tza.l1 <- readOGR("./analysis/TZA-AC-07/maps", "tza-ac-07_L1")
@mbacou
mbacou / snippets.R
Created June 6, 2013 21:39
spplot() methods
spplot(g08[g08$ADM0_NAME=="Malawi",], "toba_dens", at=qtl, lwd=.6, cex=.7,
xlab=NULL, ylab=NULL, col="gray57",
col.regions=c("white", 37:28),
main=list("Tobacco by District (kg/ha), Tanzania 2007/08", cex=.9)) +
layer(sp.polygons(g08, col="gray40", fill=0, lwd=.5))
@mbacou
mbacou / snippets.R
Created May 20, 2013 05:33
Print complex LaTeX tables.
latex(ex1[, -c(1:3)],
booktabs=TRUE, file="", center="none", table.env=FALSE,
rowname=paste(ex1$variable, ex1$unit, sep=" -- "),
cgroup=c("Farming Systems"), n.cgroup=dim(ex1)[2]-3,
rgroup=unique(ex1$AEZ5_CLAS), n.rgroup=rep(2, length(unique(ex1$AEZ5_CLAS))))
@mbacou
mbacou / snippets.R
Last active December 17, 2015 12:28
R: gfmTable()
gfmTable <- function(x, ...) {
require(ascii)
y <- capture.output(print(ascii(x, ...), type="org"))
y <- gsub("-+-", "-|-", y, fixed=T)
return(writeLines(y))
}