Skip to content

Instantly share code, notes, and snippets.

@evanemolo
Created September 20, 2012 15:37
Show Gist options
  • Save evanemolo/3756654 to your computer and use it in GitHub Desktop.
Save evanemolo/3756654 to your computer and use it in GitHub Desktop.
DWB-race-top-crimes
snf <- read.csv("http://www.jakeporway.com/teaching/data/snf_2.csv", as.is=TRUE)
crime.abbr = substr(snf$crime.suspected, 1, 3)
snf$crime.abbr = crime.abbr
race.code = c(-1,1,2,3,4,5,6)
for(i in race.code) {
# create subset
which.race = which(snf$race == i)
subset.race = snf[which.race, ]
# target crime.abbr col in subset
subset.race.crime.abbr = subset.race$crime.abbr
# keep it organized...
print(paste("racecode", i, "results:"))
# reverse sort table, target top 3 crimes, then print
print(rev(sort(table(subset.race.crime.abbr)))[1:3])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment