Skip to content

Instantly share code, notes, and snippets.

@pengelbrecht
Created September 20, 2013 13:37
Show Gist options
  • Save pengelbrecht/6637675 to your computer and use it in GitHub Desktop.
Save pengelbrecht/6637675 to your computer and use it in GitHub Desktop.
require(plyr)
columns = c(
rep("NULL", 4),
"character",
rep("NULL", 12),
"character",
rep("NULL", 33),
"character",
rep("NULL", 8)
)
strkoder = c("B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M")
#strmin = c(0, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, NA)
#strmax = c(0, 1, 4, 9, 19, 49, 99, 199, 499, 999, 5000, NA)
# this version to filter out 100+
strmin = c(0, 1, 2, 5, 10, 20, 50, 0, 0, 0, 0, NA)
strmax = c(0, 1, 4, 9, 19, 49, 99, 0, 0,0, 0, NA)
strdistrikt = c(1, 1, 1, 1, 10, 20, 20, 20, 20, 20, 20, 0)
strlookup = data.frame(strkoder, strmin, strmax, strdistrikt)
strlookup$stravg = (strlookup$strmin + strlookup$strmax) / 2
geolookup = data.frame(
c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"),
c("S", "S", "S", "S", "S", "S", "J", "J", "J", "J")
)
colnames(geolookup) <- c("postnrstart", "geodistrikt")
totalcvr <- read.csv("TOTAL_NYESTE.csv", sep = ";", fileEncoding = "Latin1", encoding = "Latin1", colClasses = columns)
colnames(totalcvr) <- c("navn", "postnr", "strkoder")
totalcvr <- merge(totalcvr, strlookup, by.x = 3, by.y = 1)
totalcvr$strkoder = NULL
totalcvr$strmin = NULL
totalcvr$strmax = NULL
totalcvr$postnr1 = substr(totalcvr$postnr, 1, 1)
totalcvr = merge(totalcvr, geolookup, by.x = 5, by.y = 1)
totalcvr$distrikt = paste(totalcvr$geodistrikt, totalcvr$strdistrikt, substr(totalcvr$navn, 1, 1), sep= "")
totalcvr$postnr1 = NULL
totalcvr$postnr = NULL
totalcvr$strdistrikt = NULL
totalcvr$geodistrikt = NULL
totalcvr$distrikt = as.factor(totalcvr$distrikt)
distriktstr = aggregate(stravg ~ distrikt, totalcvr, sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment