# Read the 'arrests.csv' file into the 'arrests' data frame
arrests <- read.csv("arrests.csv")
View(arrests)
# Read the 'value.csv' file into the 'value' data frame
value <- read.csv("value.csv")
View(value)
Data Manipulation and Analysis
# Load the 'stringr' library for string manipulation functions
library(stringr)
# Extract ZIP codes from the 'INCIDENTLOCATION' column in the 'arrests' data frame
loc <- str_extract(arrests$INCIDENTLOCATION, "\\d{5}")
table(loc)
loc
12508 13212 14521 14736 15012 15017 15028 15065 15068 15090 15104 15106
1 1 2 1 1 1 3 2 4 1 3 14
15108 15110 15116 15120 15122 15126 15132 15136 15137 15144 15145 15146
2 1 1 20 3 1 6 30 2 3 9 9
15147 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211
17 1131 16 4017 1692 792 5154 2055 3572 5 5766 1391
15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223
9795 1793 2591 116 1300 1363 51 5832 1312 2722 3543 10
15224 15225 15226 15227 15228 15231 15232 15233 15234 15235 15236 15237
1359 3 1675 447 3 4 773 1080 115 42 12 15
15238 15239 15241 15252 15260 15275 15282 15301 15317 15601 15609 15626
221 1 1 1 1 1 1 3 1 1 1 1
15629 15801 15949 16001 16057 16066 16137 16146 19320 25301 26062 26508
1 2 1 1 2 2 3 1 1 1 3 1
26554 29170 44305 44460 44634 48501 51206
1 1 1 3 1 1 1
# Select relevant columns from the 'value' data frame
valuebyloc <-value[c("PROPERTYZIP","PRICE")]
View(valuebyloc) # Display the selected columns in the 'valuebyloc' data frame
Aggregation
# Aggregate the mean of 'PRICE' by 'PROPERTYZIP' in the 'valuebyloc' data frame
meanvalbyloc<-aggregate(valuebyloc$PRICE, by = list(zip = valuebyloc$PROPERTYZIP), FUN = mean)
View(meanvalbyloc) # Display the aggregated data with column names 'zip' and 'meanVal'
# Rename columns in 'meanvalbyloc' to 'zip' and 'meanPrice'
colnames(meanvalbyloc)<-c("zip","meanPrice")
table(loc)
loc
12508 13212 14521 14736 15012 15017 15028 15065 15068 15090 15104 15106
1 1 2 1 1 1 3 2 4 1 3 14
15108 15110 15116 15120 15122 15126 15132 15136 15137 15144 15145 15146
2 1 1 20 3 1 6 30 2 3 9 9
15147 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211
17 1131 16 4017 1692 792 5154 2055 3572 5 5766 1391
15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223
9795 1793 2591 116 1300 1363 51 5832 1312 2722 3543 10
15224 15225 15226 15227 15228 15231 15232 15233 15234 15235 15236 15237
1359 3 1675 447 3 4 773 1080 115 42 12 15
15238 15239 15241 15252 15260 15275 15282 15301 15317 15601 15609 15626
221 1 1 1 1 1 1 3 1 1 1 1
15629 15801 15949 16001 16057 16066 16137 16146 19320 25301 26062 26508
1 2 1 1 2 2 3 1 1 1 3 1
26554 29170 44305 44460 44634 48501 51206
1 1 1 3 1 1 1
Data Exploration
# Create a new data frame 'incidents' with the 'loc' column
incidents <- data.frame(loc)
View(incidents) # Display the 'incidents' data frame
# Load the 'dplyr' library
library(dplyr)
# Group 'incidents' by 'loc'(the zipcode) and calculate the count of each group
incidentsbyloc <- incidents %>%
group_by(loc)%>%
summarise(Count = n())
# Display a table of counts for each 'loc' (zipcode) in 'incidents'
table(incidents$loc)
12508 13212 14521 14736 15012 15017 15028 15065 15068 15090 15104 15106
1 1 2 1 1 1 3 2 4 1 3 14
15108 15110 15116 15120 15122 15126 15132 15136 15137 15144 15145 15146
2 1 1 20 3 1 6 30 2 3 9 9
15147 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211
17 1131 16 4017 1692 792 5154 2055 3572 5 5766 1391
15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223
9795 1793 2591 116 1300 1363 51 5832 1312 2722 3543 10
15224 15225 15226 15227 15228 15231 15232 15233 15234 15235 15236 15237
1359 3 1675 447 3 4 773 1080 115 42 12 15
15238 15239 15241 15252 15260 15275 15282 15301 15317 15601 15609 15626
221 1 1 1 1 1 1 3 1 1 1 1
15629 15801 15949 16001 16057 16066 16137 16146 19320 25301 26062 26508
1 2 1 1 2 2 3 1 1 1 3 1
26554 29170 44305 44460 44634 48501 51206
1 1 1 3 1 1 1
# Create a data frame 'incidentsbyzip' from the table of counts
incidentcounts<-table(incidents$loc)
incidentsbyzip<-as.data.frame(incidentcounts)
View(incidents)
View(incidentsbyzip)
# Rename columns in 'incidentsbyzip' to 'zip' and 'incidentCounts'
colnames(incidentsbyzip)<-c("zip","incidentCounts")
Merge and Analysis
# Merge 'meanvalbyloc' and 'incidentsbyzip' by the 'zip' column, including all entries
zipStats <-merge(meanvalbyloc,incidentsbyzip, by="zip", all = TRUE)
# Create a data frame 'zipstats' from the merged result
zipstats <- data.frame(merge(meanvalbyloc, incidentsbyzip, by = "zip"))
View(zipstats)
Plotting
# Plot a scatter plot of meanPrice against incidentCounts in 'zipstats'
plot(zipstats$meanPrice,zipstats$incidentCounts)
Linear Regression
# Fit a linear regression model with 'incidentCounts' as the response variable and 'meanPrice' as the predictor
model <- lm(incidentCounts~meanPrice, data = zipStats)
summary(model)
Call:
lm(formula = incidentCounts ~ meanPrice, data = zipStats)
Residuals:
9 18 71 117
-3.31010 -1.36049 4.68634 -0.01574
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.370e+00 2.438e+00 1.792 0.215
meanPrice -8.794e-07 1.278e-06 -0.688 0.563
Residual standard error: 4.17 on 2 degrees of freedom
(147 observations deleted due to missingness)
Multiple R-squared: 0.1914, Adjusted R-squared: -0.2129
F-statistic: 0.4734 on 1 and 2 DF, p-value: 0.5625