Repository navigation
Expand file tree
/
Copy pathCapstoneAnalysisPt2-Wrangling.R
More file actions
119 lines (90 loc) · 2.71 KB
/
Copy pathCapstoneAnalysisPt2-Wrangling.R
File metadata and controls
119 lines (90 loc) · 2.71 KB
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
#Setup
library(tidyverse)
library(digest)
library(lubridate)
# load data
anondata <- read_csv("data/AnonData.csv")
# transform to logical data frame
voted_df <-
anondata %>%
select(matches("^E[0-9]+")) %>%
mutate_all(~ !is.na(.x)) %>%
print()
nm <- gsub("^E", "", names(voted_df))
nm <- as.character(as.Date(nm, "%m%d%y"))
names(voted_df) <- nm
# only valid dates
voted_df <- voted_df[, !is.na(nm)]
# update names
nm <- names(voted_df)
names(nm) <- nm
# checks
stopifnot(!anyNA(nm))
stopifnot(!anyNA(anondata$REG_DT))
registered_chr <- as.character(anondata$REG_DT)
registered_df <-
registered_chr %>%
purrr::map_dfr(~ as.list(nm >= .x))
df <- tibble(
REG_DT = anondata$REG_DT,
n_votes = rowSums(voted_df),
n_reg = rowSums(registered_df)
) %>%
mutate(vote_fraction = n_votes / n_reg) %>%
print()
# Verified in Excel using n_votes =COUNTIF($AR2:$DX2,">0"), n_reg =COUNTIF($AR$1:$DX$1,">"&$U2), vote_fraction = =EA2
write_csv(df, path = "data/VoteFraction.csv", col_names = TRUE)
#mutate(anondata, )
## START NEW SCRIPT USE R_MARKDOWN TO RECORD PROJECT- MULT VERSIONS
# Change all dates in file to international date standard
anon_names <- names(anondata)
anon_index <- 44:139
anon_elect <- anon_names[anon_index]
anon_nonE <- sub("^E", "", anon_elect)
anon_date <- as.Date(anon_nonE, "%m%d%y")
anon_date
anon_char_date <- as.character(anon_date)
anon_char_date[is.na(anon_char_date)] <- "deleteme"
anon_names[anon_index] <- anon_char_date
anon_names
names(anondata) <- anon_names
names(anondata)
## Remove "deleteme" columns
#NewData <- select(anondata, -deleteme)
#dat[ , !names(dat) %in% c("foo","bar")]
#setdiff(names(dat), c("foo", "bar"))
# Without delement columns names
# names(anondata) <- setdiff(names(anondata), "deleteme")
index <- which(names(anondata) == "deleteme")
anondata <- anondata[, -index]
#index
#length(index)
#139-11
# Continue EDA
summary(anondata[,44:128]) #Verify same columns after EDA update
str(anondata)
head(anondata)
names(anondata)
#Test Visuals:
ggplot(anondata, aes(x = PARTY)) +
geom_bar()
anondata <- anondata %>%
as_tibble() %>%
mutate(AGE = as.integer((as.Date('2018-09-18') - BIRTHDATE)/365))
#select(AGE) # Use lubridate function
ggplot(anondata, aes(x= GENDER)) +
geom_bar()
ggplot(anondata, aes(x= ADDR_CITY)) +
geom_bar() +
coord_flip()
#ggplot(anondata, aes(x= )) +
# geom_histogram()
df <- anondata[,44:128]
df_names <- names(df)
names(df_names) <- df_names
df_TrueFalse <- anondata$REG_DT %>%
map(~ df_names > as.character(.x))
# Presidential Election Dates
Pres_Elec_DT <- c('1988-11-08', '1992-11-03', '1996-11-05', '2000-11-07', '2004-11-02', '2008-11-04', '2012-11-06', '2016-11-08')
g <- ggplot(anondata, aes(PARTY))
g + geom_bar(aes(fill = PARTY))