Created
February 5, 2013 00:30
-
-
Save lc0/4711078 to your computer and use it in GitHub Desktop.
Computing for data analysis. Week4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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