-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path08-getting_data.R
More file actions
78 lines (60 loc) · 1.94 KB
/
Copy path08-getting_data.R
File metadata and controls
78 lines (60 loc) · 1.94 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
# load libraries
library(tidyverse)
library(rvest)
# getting data from a html page
url <- "https://en.wikipedia.org/wiki/University_of_Arizona"
# to read in the html page
uarizona_wiki_page <- read_html(url)
# parse html page for table
uarizona_wiki_page %>%
html_nodes("table")
# parse html for wikitable class
ua_wiki_tables <- uarizona_wiki_page %>%
html_nodes(".wikitable")
# check first table in wiki_tables
ua_wiki_tables[[1]] %>%
html_table(fill = TRUE)
# check the second table
ua_wiki_tables[[2]] %>%
html_table(fill = TRUE)
# check the third table
fall_freshman_stats <- ua_wiki_tables[[3]] %>%
html_table(fill = TRUE)
# assign a name to first column
colnames(fall_freshman_stats)[1] <- "type"
# pivot year columns to gather them into one column/variable called "year"
fall_fresh_longer <- fall_freshman_stats %>%
pivot_longer(cols = "2017":"2013",
names_to = "year")
# filter to keep value that is NOT NA
fall_fresh_clean <- fall_fresh_longer %>%
filter(!is.na(value))
# inspect the data
glimpse(fall_fresh_clean)
# plot the data as is
# start with fall_fresh_clean and then
# filter to keep type only Applicants, Admits, Enrolled
# plot the data, mapping x to year, y to value, and color type
fall_fresh_clean %>%
filter(type %in% c("Applicants", "Admits", "Enrolled")) %>%
ggplot(aes(x = year,
y = value,
color = type)) +
geom_point() +
geom_line(aes(group = type))
# fix the value column to be numeric (instead of character)
# using parse_number()
fall_fresh_clean <- fall_fresh_clean %>%
mutate(value = parse_number(value))
# inspect data
glimpse(fall_fresh_clean)
# plot it again with data frame fixed
fall_fresh_clean %>%
filter(type %in% c("Applicants", "Admits", "Enrolled")) %>%
ggplot(aes(x = year,
y = value,
color = fct_reorder(type, value, .desc = TRUE))) +
geom_point() +
geom_line(aes(group = type)) +
labs(y = "student count",
color = "")