Skip to content

Instantly share code, notes, and snippets.

View mhweber's full-sized avatar

Marc Weber mhweber

View GitHub Profile
@mhweber
mhweber / Lookup.R
Last active October 1, 2015 19:10
This code takes a lookup table and applies it to a data frame, updating only values for records that occur in the lookup table using indexing and match
# Create data frame 1
x = c("ID1","ID2","ID3","ID4","ID5")
y = c("C1","C2","C3","C4","C5")
d1 = data.frame("SiteID" = x, "Value" = y)
d1
# Create lookup table
x = c("ID2","ID5")
y = c("C5","C2")
lookup = data.frame("SiteID" = x, "Value" = y)
lookup
@mhweber
mhweber / LineLengthsPolys.R
Last active August 29, 2015 14:07
This piece gets some summary statistics on spatial line data frame lengths within spatial polygon data frames in R
library(rgeos);library( rgdal)
polys <- readOGR('.','polys')
lines <- readOGR('.','lines')
if (proj4string(polys) == proj4string(lines){
plot(polys); plot(lines, add=T, color='Blue')
lines$LENGTHKM <- SpatialLinesLengths(lines)*0.001 # assumes lines are in projection using meters
line.sum <- over(polys, lines[6], fn=sum)
line.mean <- over(polys, lines[6], fn=mean)
line.min <- over(polys, lines[6], fn=min)
line.max <- over(polys, lines[6], fn=max)