Skip to content

Commit 4fb36ce

Browse files
committed
finished week4 of compdata
1 parent 1c8e78b commit 4fb36ce

File tree

7 files changed

+1431
-0
lines changed

7 files changed

+1431
-0
lines changed

compdata/week4/ProgAssignment4.pdf

92.2 KB
Binary file not shown.

compdata/week4/agecount.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
agecount <- function(age = NULL) {
2+
## Check that "age" is non-NULL; else throw error
3+
if(is.null(age)){
4+
stop("Cause cannot be NULL!")
5+
}
6+
7+
## Read "homicides.txt" data file
8+
homicides <- readLines("homicides.txt")
9+
10+
regex <- paste(" ", age, " year", sep = "")
11+
12+
## Extract ages of victims; ignore records where no age is given
13+
## Return integer containing count of homicides for that age
14+
length(grep(regex, homicides))
15+
}

compdata/week4/count.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
count <- function(cause = NULL) {
2+
## Check that "cause" is non-NULL; else throw error
3+
if(is.null(cause)){
4+
stop("Cause cannot be NULL!")
5+
}
6+
7+
## Check that specific "cause" is allowed; else throw error
8+
range <- c("asphyxiation", "blunt force", "other", "shooting", "stabbing", "unknown")
9+
10+
if(!is.element(cause, range)){
11+
stop(paste("Invalid cause, must be one of ", paste(range, collapse = ', ')))
12+
}
13+
14+
## Read "homicides.txt" data file
15+
homicides <- readLines("homicides.txt")
16+
17+
firstLetter <- substr(cause, 0, 1)
18+
regex <- paste("Cause: [", firstLetter, toupper(firstLetter), "]", substr(cause, 2, nchar(cause)), sep = "")
19+
20+
## Extract causes of death
21+
## Return integer containing count of homicides for that cause
22+
length(grep(regex, homicides))
23+
}

compdata/week4/hmwk4.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#hmwk4
2+
getwd()
3+
setwd("compdata/week4")
4+
homicides <- readLines("homicides.txt")
5+
6+
count("other")
7+
num <- count("unknown")
8+
print(num)
9+
10+
agecount(3)
11+
agecount(1)
12+
num <- agecount(21)
13+
print(num)
14+
15+
16+
#submit
17+
source("http://spark-public.s3.amazonaws.com/compdata/scripts/submitscript4.R")
18+
submit()

compdata/week4/homicides.txt

Lines changed: 1250 additions & 0 deletions
Large diffs are not rendered by default.

compdata/week4/quiz4.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
library(grDevices)
2+
x <- colorRamp(c("red", "blue"))
3+
x
4+
y <- colorRampPalette(c("red", "yellow"))
5+
y
6+
7+
rgb(0, 0, 0, 0.2)
8+
9+
s <- "she likes rum raisin after running hard"
10+
regex <- "^s(.*?)r"
11+
regexpr(regex, s)
12+
13+
?setOldClass

compdata/week4/week4.R

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#Colors and Plots
2+
library(grDevices)
3+
pal <- colorRamp(c("red", "blue"))
4+
pal
5+
pal(0)
6+
pal(1)
7+
pal(0.5)
8+
pal(seq(0, 1, len = 10))
9+
10+
pal <- colorRampPalette(c("red", "yellow"))
11+
pal(2)
12+
pal(10)
13+
14+
library(RColorBrewer)
15+
cols <- brewer.pal(3, "BuGn")
16+
cols
17+
pal <- colorRampPalette(cols)
18+
image(volcano, col = pal(20))
19+
20+
x <- rnorm(10000)
21+
y <- rnorm(10000)
22+
smoothScatter(x, y)
23+
24+
plot(x, y, pch = 19)
25+
plot(x, y, col = rgb(0, 0, 0, 0.2), pch = 19)
26+
27+
#Dates and Times
28+
x <- as.Date("1970-01-01")
29+
x
30+
31+
?unclass
32+
unclass(x)
33+
unclass(as.Date("1970-01-02"))
34+
35+
x <- Sys.time()
36+
x
37+
38+
p <- as.POSIXlt(x)
39+
names(unclass(p))
40+
41+
p$sec
42+
43+
datestring <- c("January 10, 2012 10:40", "December 9, 2011 9:10")
44+
x <- strptime(datestring, "%B %d, %Y %H:%M")
45+
x
46+
47+
x <- as.Date("2012-01-01")
48+
y <- strptime("9 Jan 2011 11:34:21", "%d %b %Y %H:%M:%S")
49+
x - y
50+
51+
x <- as.POSIXlt(x)
52+
x - y
53+
54+
x <- as.Date("2012-03-01")
55+
y <- as.Date("2012-02-28")
56+
x - y
57+
58+
x <- as.POSIXct("2012-10-25 01:00:00")
59+
y <- as.POSIXct("2012-10-25 06:00:00", tz = "GMT")
60+
y - x
61+
62+
#Regex
63+
#
64+
#^ matches start of a line
65+
#
66+
#$ matches end of a line
67+
#
68+
#We can list a set of characters we will accept at a given point in the match
69+
#[Bb][Uu][Ss][Hh]
70+
#
71+
#range of letters
72+
#[a-z] or [a-zA-Z]
73+
#
74+
#. refers to any character
75+
#
76+
#| means or
77+
#
78+
#? means optional
79+
#
80+
#* means 0 or many
81+
#
82+
#+ means 1 or many
83+
#
84+
#{} means interval, as in {1,5}
85+
86+
#Bmore Homicides
87+
setwd("compdata/week4")
88+
89+
#Regex in R
90+
homicides <- readLines("homicides.txt")
91+
homicides[1]
92+
str(homicides)
93+
94+
length(grep("iconHomicideShooting", homicides))
95+
length(grep("iconHomicideShooting|icon_homicide_shooting", homicides))
96+
length(grep("Cause: shooting", homicides))
97+
length(grep("Cause: [Ss]hooting", homicides))
98+
length(grep("[Ss]hooting", homicides))
99+
100+
grep("^New", state.name)
101+
grep("^New", state.name, value = TRUE)
102+
grepl("^New", state.name)
103+
104+
regexpr("<dd>[F|f]ound(.*)</dd>", homicides[1:10])
105+
substr(homicides[1], 177, 177 + 93 - 1)
106+
107+
r <- regexec("<dd>[F|f]ound on (.*?)</dd>", homicides)
108+
m <- regmatches(homicides, r)
109+
m
110+
dates <- sapply(m, function(x) x[2])
111+
dates <- as.Date(dates, "%B %d, %Y")
112+
hist(dates, "month", freq = TRUE)

0 commit comments

Comments
 (0)