Skip to content

Instantly share code, notes, and snippets.

@mbacou
mbacou / snippets.R
Created March 25, 2013 16:24
R: Find unmatched observations
# Which are the rows in dt1 that aren't in dt2.
which(is.na(dt2[dt1, which=TRUE, mult="first"]))
@mbacou
mbacou / snippets.R
Created March 25, 2013 16:25
R: Read head/tail of large files
# Read head/tail of large files
readLines("./2010 BMGF/data/Segments/cell5m_d14.txt", n=10)
@mbacou
mbacou / snippets.R
Created March 25, 2013 16:27
R: Export multiple graphs to PNG or SVG in one step
# Export multiple graphs to PNG or SVG at once
png("/out/YieldTarget_%d.png")
dotplot(cropSPAM~value,
plot[!plot$cropSPAM %in% c("BANP", "SUGC"),],
group=variable,
main=list("Mean GAEZ Low and High Input Yield Potentials \nagainst Mean SPAM Yield", cex=.9),
xlab="yield (kg/ha)",
pch=c(1,1,3),
cex=1.2,
col=c("green", "red", "orange"),
@mbacou
mbacou / snippets.R
Created March 25, 2013 16:29
R: Draw random dot-density maps
# Random dot density maps
dots.rand <- dotsInPolys(gha, gha@data$total, f="random")
plot(dots.rand,
pch=19, cex=0.4, col="red",
add=TRUE)
text(coordinates(gha.reg),
labels=gha.reg@data$REGION,
adj=c(0, 0), cex=.9, col="black", xpd=NA)
@mbacou
mbacou / snippets.R
Created March 25, 2013 16:31
R: Draw and export circle maps to PDF
## Export circle map to PDF
pdf("US04Election-PopBin.pdf")
# Draw the USA outline
map('usa', fill=T, col="white", bg="darkgray")
# Create the colors for the states
col <- rep(rgb(.1,.1,.1,.2), length(counties$names))
col[election$order] <- bluered(election$Bush,election$Kerry, .4, graded=F)
# Plot states without borders (should work with map, see help for fill
# argument, but cannot get it working)
@mbacou
mbacou / snippets.R
Created March 25, 2013 16:33
R: Dissolve shapes across attributes
# Dissolve map into regions and farming systems
gha.fs <- unionSpatialPolygons(gha, interaction(gha@data$REGION, gha@data$FS))
@mbacou
mbacou / snippets.R
Last active December 15, 2015 09:29
R: Compute shape area (with projection)
# Compute polygon area
fs.gha@data$area <- sapply(fs.gha@polygons, slot, "area")
@mbacou
mbacou / snippets.R
Created March 25, 2013 16:35
R: Print table for quick documentation
# Print for documentation
print(ascii(x,
align=c("l", "r", "l", "r"),
include.rownames=F,
frame="topbot",
size=NULL),
type = "rest")
@mbacou
mbacou / snippets.R
Created May 19, 2013 23:25
R: htmlTable()
#' This is a simple function for outputting a more advanced
#' table than xtable allows. It's aim is to provide the Hmisc
#' latex() colgroup and rowgroup functions.
#'
#' @param x The matrix/data.frame with the data
#' @param headings a vector of character strings specifying column
#' headings, defaulting to \code{x}'s \code{colnames}
#' @param align a character strings specifying column alignments, defaulting to
#' \code{paste(rep('c',ncol(x)),collapse='')} to center.
#' You may specify \code{align='c|c'} and other LaTeX tabular formatting.
@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))
}