-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgu2.R
165 lines (117 loc) · 4.06 KB
/
cgu2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#######################################
# Loren Collingwood #
# UC Riverside #
# DATE: 5/22/2019 #
# Webscraping with rvest #
# Topic 1: Immigrant Detention Deaths #
#######################################
rm(list=ls())
############
# Packages #
############
check.packages <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
# Check to see if packages are installed, and then load them
packages<-c("rvest","stringr","striprtf", "gsubfn", "tidyverse","readxl", "quanteda", "descr",
"ggplot2", "readxl","writexl", "topicmodels", "devtools", "lubridate", "svMisc",
"data.table", "tm")
check.packages(packages)
# Read in death data located on capital and main website #
deaths <- read_html("https://capitalandmain.com/mapping-death")
class(deaths)
#################################################
# Read in Table-type information #
#################################################
# Like much of webscraping, the data are unruly #
# Welcome to text anaylysis #
deaths <- (html_table(deaths, fill=T))
##################################################
# Take the first list which is data.frame object #
##################################################
deaths <- deaths[[1]]
# Label column name #
colnames(deaths)[1]<- "Name"
############
# CLEANING #
############
# Recode for checking purposes
deaths$death_r <- ifelse(deaths[,1]== "FINAL CAUSE OF DEATH", 1, 0)
# Check the messed up pattern -- DF not quite what wanted, so need to delete a few `rogue` cases
which(deaths$death_r==1)
# Drop these few here that are messing up the 1:10 flow #
deaths <- deaths[-412,]
deaths <- deaths[-1293,]
#################
# Set up Parser #
#################
splitter <- list()
n <- 1880
############################
# Create splitter variable #
############################
for (i in 1:(n/10)) splitter[[i]] <- rep(i, 10)
splitter <- unlist(splitter); length(splitter)
########################################
# Subset just to the necessary columns #
########################################
tester <- deaths[1:n,c("Name", "Title", "death_r")]
tester <- data.frame(tester, splitter, stringsAsFactors = F)
##############################
# Split data into n/10 lists #
##############################
tester_list <- split(tester, tester$splitter)
# Take a peek #
tester_list
##############################
# Function: death_list_split #
##############################
death_list_split <- function(x){
# Relabels
x[1,1] <- "Name"
# Returns just the first 2 columns
x[, c("Name", "Title")]
}
##################################
# Execute Function with lapply() #
##################################
out <- lapply(tester_list, death_list_split)
######################################################
# Bind columns; transpose, and put into data.frame() #
######################################################
out <- data.frame(t(dplyr::bind_cols(out)), stringsAsFactors=F)
##################
# Clean the data #
##################
colnames(out) <- out[1,]
rownames(out) <- NULL
#############################
# Drop Rows you don't need #
#############################
drops <- seq(1, nrow(out), 2)
out <- out[-drops,]
#############################
# Verify Everything is good #
#############################
tail(out)
##########################
# Recode a few Variables #
##########################
out$SEX[out$SEX=="SEX"] <- "M"
out$SEX[out$SEX=="W"] <- "F"
table(out$SEX)
#################################
# Date variables, year of Death #
#################################
out$dob <- lubridate::mdy(out$`DATE OF BIRTH`) # some missing data
out$yob <- lubridate::year(out$dob)
out$dod <- lubridate::mdy(out$`DATE OF DEATH`) # 1 missing data
out$yod <- lubridate::year(out$dod)
out$age_death <- with(out, yod - yob)
#################
# Write to Disk #
#################
write.csv(out, "detention_deaths.csv", row.names=F)