Skip to content

Instantly share code, notes, and snippets.

@lc0
Created February 5, 2013 00:30
Show Gist options
  • Save lc0/4711078 to your computer and use it in GitHub Desktop.
Save lc0/4711078 to your computer and use it in GitHub Desktop.
Computing for data analysis. Week4
agecount <- function(age = NULL) {
if (is.null(age)){
stop("age couldn't be NULL")
}
homicides <- readLines("homicides.txt")
r <- regexec("(\\d+) years old", homicides)
m <- regmatches(homicides, r)
causes <- sapply(m, function(x) as.integer(x[2]))
return(length(which(causes==age)))
}
count <- function(cause = NULL) {
if (is.null(cause)){
stop("cause couldn't be NULL")
}
alloved <- c("asphyxiation", "blunt force", "other", "shooting",
"stabbing", "unknown")
if (cause %in% alloved) {
homicides <- readLines("homicides.txt")
r <- regexec("Cause: ([^<]+)", homicides)
m <- regmatches(homicides, r)
causes <- sapply(m, function(x) tolower(x[2]))
return(length(which(causes==cause)))
}
else {
stop("cause isn't in the list of alloved")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment