Use theflightsdata from packagenycflights13to answer thefollowing
questions.
data("flights")
A)
arriveLateFlights <- flights %>%
mutate(arriveLate = ifelse(arr_delay > 5, "Late", "On Time")) %>%
group_by(month, arriveLate) %>%
summarize(totalFlights = n())
## `summarise()` has grouped output by 'month'. You can override using the
## `.groups` argument.
print(arriveLateFlights)
## # A tibble: 36 × 3
## # Groups: month [12]
## month arriveLate totalFlights
## <int> <chr> <int>
## 1 1 Late 8988
## 2 1 On Time 17410
## 3 1 <NA> 606
## 4 2 Late 8119
## 5 2 On Time 15492
## 6 2 <NA> 1340
## 7 3 Late 9033
## 8 3 On Time 18869
## 9 3 <NA> 932
## 10 4 Late 10544
## # ℹ 26 more rows
B)
trafficByCarrier <- flights %>%
group_by(month, carrier) %>%
summarize(traffic_percent = n() / nrow(flights) * 10)
## `summarise()` has grouped output by 'month'. You can override using the
## `.groups` argument.
horizontal <- trafficByCarrier %>%
mutate(traffic_percent = scales::percent(traffic_percent)) %>%
pivot_wider(names_from = month, values_from = traffic_percent)
horizontal
## # A tibble: 16 × 13
## carrier `1` `2` `3` `4` `5` `6` `7` `8` `9` `10` `11`
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 9E 4.6708% 4.332… 4.83… 4.48… 4.34… 4.26… 4.43… 4.32… 4.57… 4.96… 4.73…
## 2 AA 8.2963% 7.473… 8.27… 8.08… 8.32… 8.18… 8.55… 8.48… 7.76… 8.06… 7.65…
## 3 AS 0.1841% 0.166… 0.18… 0.17… 0.18… 0.17… 0.18… 0.18… 0.17… 0.18… 0.15…
## 4 B6 13.1452% 12.18… 14.1… 13.4… 13.5… 13.7… 14.7… 14.7… 12.7… 12.9… 12.7…
## 5 DL 10.9568% 10.22… 12.4… 12.1… 12.1… 12.2… 12.6… 12.8… 11.5… 12.1… 11.4…
## 6 EV 12.3851% 11.36… 14.0… 13.5… 14.3… 13.2… 13.7… 13.5… 14.0… 14.5… 13.2…
## 7 F9 0.1752% 0.145… 0.16… 0.16… 0.17… 0.16… 0.17… 0.16… 0.17… 0.16… 0.18…
## 8 FL 0.9739% 0.878… 0.93… 0.92… 0.96… 0.74… 0.78… 0.78… 0.75… 0.70… 0.59…
## 9 HA 0.0920% 0.083… 0.09… 0.08… 0.09… 0.08… 0.09… 0.09… 0.07… 0.06… 0.07…
## 10 MQ 6.7434% 6.069… 6.69… 6.56… 6.78… 6.46… 6.71… 6.71… 6.55… 6.61… 6.10…
## 11 OO 0.0030% <NA> <NA> <NA> <NA> 0.00… <NA> 0.01… 0.05… <NA> 0.01…
## 12 UA 13.7688% 12.90… 14.7… 14.9… 14.7… 14.7… 15.0… 15.2… 13.9… 15.0… 14.4…
## 13 US 4.7569% 4.608… 5.11… 5.12… 5.30… 5.15… 5.30… 5.28… 5.04… 5.48… 5.04…
## 14 VX 0.9383% 0.804… 0.90… 1.38… 1.47… 1.42… 1.45… 1.45… 1.34… 1.40… 1.33…
## 15 WN 2.9575% 2.705… 2.96… 2.90… 2.98… 3.05… 3.19… 3.10… 2.99… 3.24… 3.06…
## 16 YV 0.1366% 0.142… 0.05… 0.11… 0.14… 0.14… 0.24… 0.19… 0.12… 0.19… 0.14…
## # ℹ 1 more variable: `12` <chr>
C)
flights <- flights %>%
mutate(date = as.Date(paste(year, month, day, sep = "-")))
latestDepartures <- flights %>%
group_by(year, month) %>%
filter(dep_time == max(dep_time, na.rm = TRUE)) %>%
select(year, month, day, dep_time, sched_dep_time, dep_delay, arr_time, sched_arr_time)
latestDepartures
## # A tibble: 35 × 8
## # Groups: year, month [12]
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 7 2359 2359 0 506 437
## 2 2013 1 12 2359 2359 0 429 437
## 3 2013 1 13 2359 2130 149 435 218
## 4 2013 1 18 2359 2359 0 439 437
## 5 2013 1 19 2359 2359 0 437 444
## 6 2013 1 25 2359 2359 0 500 444
## 7 2013 10 30 2400 2359 1 327 337
## 8 2013 11 27 2400 2359 1 515 445
## 9 2013 12 5 2400 2359 1 427 440
## 10 2013 12 9 2400 2359 1 432 440
## # ℹ 25 more rows
2
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.4
## ✔ ggplot2 3.4.3 ✔ stringr 1.5.0
## ✔ lubridate 1.9.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
usefulDataset <- read.csv('multipleChoiceResponses.csv')
A)
usefulVertical <- usefulDataset %>%
pivot_longer(cols = starts_with("LearningPlatformUsefulness"), names_to = "learning_platform", values_to = "usefulness") %>%
mutate(learning_platform = str_remove(learning_platform, "LearningPlatformUsefulness")) %>%
group_by(learning_platform, usefulness) %>%
summarise(count = n()) %>%
filter(!is.na(count) & !is.na(usefulness))
## `summarise()` has grouped output by 'learning_platform'. You can override using
## the `.groups` argument.
usefulVertical
## # A tibble: 54 × 3
## # Groups: learning_platform [18]
## learning_platform usefulness count
## <chr> <chr> <int>
## 1 Arxiv Not Useful 37
## 2 Arxiv Somewhat useful 1038
## 3 Arxiv Very useful 1316
## 4 Blogs Not Useful 45
## 5 Blogs Somewhat useful 2406
## 6 Blogs Very useful 2314
## 7 College Not Useful 101
## 8 College Somewhat useful 1405
## 9 College Very useful 1853
## 10 Communities Not Useful 16
## # ℹ 44 more rows
B)
usefulVerticalPercentage <- read.csv('multipleChoiceResponses.csv')
percentageUsefulVertical <- usefulVerticalPercentage %>%
pivot_longer(cols = starts_with("LearningPlatformUsefulness"), names_to = "learning_platform", values_to = "usefulness") %>%
mutate(learning_platform = str_remove(learning_platform, "LearningPlatformUsefulness")) %>%
group_by(learning_platform) %>%
summarise(count = sum(sum(usefulness != "Not Useful" & !is.na(usefulness))), total = sum(!is.na(usefulness)),
perc_not_useful = sum(usefulness != "Not Useful" & !is.na(usefulness)) / total)
percentageUsefulVertical
## # A tibble: 18 × 4
## learning_platform count total perc_not_useful
## <chr> <int> <int> <dbl>
## 1 Arxiv 2354 2391 0.985
## 2 Blogs 4720 4765 0.991
## 3 College 3258 3359 0.970
## 4 Communities 1126 1142 0.986
## 5 Company 940 981 0.958
## 6 Conferences 2063 2182 0.945
## 7 Courses 5945 5992 0.992
## 8 Documentation 2279 2321 0.982
## 9 Friends 1530 1581 0.968
## 10 Kaggle 6527 6583 0.991
## 11 Newsletters 1033 1089 0.949
## 12 Podcasts 1090 1214 0.898
## 13 Projects 4755 4794 0.992
## 14 SO 5576 5640 0.989
## 15 Textbook 4112 4181 0.983
## 16 TradeBook 324 333 0.973
## 17 Tutoring 1394 1426 0.978
## 18 YouTube 5125 5229 0.980
C)
ggplot(percentageUsefulVertical, aes(x = learning_platform, y = perc_not_useful)) +
geom_segment(aes(x = learning_platform, xend = learning_platform, y = 0, yend = perc_not_useful), color = "black") +
geom_point(color = "black") +
theme_minimal() +
theme(panel.grid = element_blank(),
panel.background = element_rect(fill = "#ebebeb"),
panel.grid.major = element_line(color = "white", linetype = "solid"),
panel.grid.minor = element_line(color = "white", linetype = "solid"),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank())
