There and Back Again competition entry https://shiny.petras.space/commute/
rstats
rshiny
census
competition
leaflet
javascript
stats-nz

process.R 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Process csv data into an RData file to be loaded by the shiny application
  2. library(readr)
  3. library(dplyr)
  4. education_travel <- read_csv("travel-education.csv")
  5. work_travel <- read_csv("travel-work.csv")
  6. work_travel %>% select(
  7. res_code = SA2_code_usual_residence_address,
  8. res_name = SA2_name_usual_residence_address,
  9. res_east = SA2_usual_residence_easting,
  10. res_north = SA2_usual_residence_northing,
  11. work_code = SA2_code_workplace_address,
  12. work_name = SA2_name_workplace_address,
  13. work_east = SA2_workplace_easting,
  14. work_north = SA2_workplace_northing,
  15. private = Drive_a_private_car_truck_or_van,
  16. passenger = Passenger_in_a_car_truck_van_or_company_bus,
  17. walk = Walk_or_jog,
  18. bicycle = Bicycle,
  19. company = Drive_a_company_car_truck_or_van,
  20. bus = Public_bus,
  21. train = Train,
  22. ferry = Ferry,
  23. other = Other,
  24. home = Work_at_home,
  25. total = Total
  26. ) -> work_simp
  27. work_simp %>%
  28. group_by(res_code,
  29. res_name,
  30. res_east,
  31. res_north) %>%
  32. summarise(
  33. private = sum(ifelse(private < 0, 0, private)),
  34. passenger = sum(ifelse(passenger < 0, 0, passenger)),
  35. walk = sum(ifelse(walk < 0, 0, walk)),
  36. bicycle = sum(ifelse(bicycle < 0, 0, bicycle)),
  37. company = sum(ifelse(company < 0, 0, company)),
  38. bus = sum(ifelse(bus < 0, 0, bus)),
  39. train = sum(ifelse(train < 0, 0, train)),
  40. ferry = sum(ifelse(ferry < 0, 0, ferry)),
  41. other = sum(ifelse(other < 0, 0, other)),
  42. home = sum(ifelse(home < 0, 0, home)),
  43. total = sum(ifelse(total < 0, 0, total)), .groups="drop"
  44. ) -> work_from
  45. work_simp %>%
  46. group_by(work_code,
  47. work_name,
  48. work_east,
  49. work_north) %>%
  50. summarise(
  51. private = sum(ifelse(private < 0, 0, private)),
  52. passenger = sum(ifelse(passenger < 0, 0, passenger)),
  53. walk = sum(ifelse(walk < 0, 0, walk)),
  54. bicycle = sum(ifelse(bicycle < 0, 0, bicycle)),
  55. company = sum(ifelse(company < 0, 0, company)),
  56. bus = sum(ifelse(bus < 0, 0, bus)),
  57. train = sum(ifelse(train < 0, 0, train)),
  58. ferry = sum(ifelse(ferry < 0, 0, ferry)),
  59. other = sum(ifelse(other < 0, 0, other)),
  60. home = sum(ifelse(home < 0, 0, home)),
  61. total = sum(ifelse(total < 0, 0, total)), .groups="drop"
  62. ) -> work_to
  63. edu_simp <- education_travel %>% select(
  64. res_code = SA2_code_usual_residence_address,
  65. res_name = SA2_name_usual_residence_address,
  66. res_east = SA2_usual_residence_easting,
  67. res_north = SA2_usual_residence_northing,
  68. edu_code = SA2_code_educational_address,
  69. edu_name = SA2_name_educational_address,
  70. edu_east = SA2_educational_easting,
  71. edu_north = SA2_educational_northing,
  72. drive = Drive_a_car_truck_or_van,
  73. passenger = Passenger_in_a_car_truck_or_van,
  74. walk = Walk_or_jog,
  75. bicycle = Bicycle,
  76. scholbus = School_bus,
  77. pubbus = Public_bus,
  78. train = Train,
  79. ferry = Ferry,
  80. other = Other,
  81. home = Study_at_home,
  82. total = Total
  83. )
  84. edu_simp %>% group_by(res_code, res_name, res_east, res_north) %>%
  85. summarise(
  86. drive = sum(ifelse(drive < 0, 0, drive)),
  87. passenger = sum(ifelse(passenger < 0, 0, passenger)),
  88. walk = sum(ifelse(walk < 0, 0, walk)),
  89. bicycle = sum(ifelse(bicycle < 0, 0, bicycle)),
  90. scholbus = sum(ifelse(scholbus < 0, 0, scholbus)),
  91. pubbus = sum(ifelse(pubbus < 0, 0, pubbus)),
  92. train = sum(ifelse(train < 0, 0, train)),
  93. ferry = sum(ifelse(ferry < 0, 0, ferry)),
  94. other = sum(ifelse(other < 0, 0, other)),
  95. home = sum(ifelse(home < 0, 0, home)),
  96. total = sum(ifelse(total < 0, 0, total)), .groups="drop"
  97. ) -> edu_from
  98. edu_simp %>% group_by(edu_code, edu_name, edu_east, edu_north) %>%
  99. summarise(
  100. drive = sum(ifelse(drive < 0, 0, drive)),
  101. passenger = sum(ifelse(passenger < 0, 0, passenger)),
  102. walk = sum(ifelse(walk < 0, 0, walk)),
  103. bicycle = sum(ifelse(bicycle < 0, 0, bicycle)),
  104. scholbus = sum(ifelse(scholbus < 0, 0, scholbus)),
  105. pubbus = sum(ifelse(pubbus < 0, 0, pubbus)),
  106. train = sum(ifelse(train < 0, 0, train)),
  107. ferry = sum(ifelse(ferry < 0, 0, ferry)),
  108. other = sum(ifelse(other < 0, 0, other)),
  109. home = sum(ifelse(home < 0, 0, home)),
  110. total = sum(ifelse(total < 0, 0, total)), .groups="drop"
  111. ) -> edu_to
  112. tencols <- c("#f85654", "#e31a1c", "#1f78b4", "#6a3d9a", "#b2df8a",
  113. "#33a02c", "#fdbf6f", "#ff7f00", "#cab2d6", "#af8ac1")
  114. work_from$MAX <- work_from %>% select(private:home) %>% as.matrix() %>%
  115. apply(1, function(x) {
  116. ifelse(max(x) <= 0, NA, which.max(x))
  117. })
  118. work_to$MAX <- work_to %>% select(private:home) %>% as.matrix() %>%
  119. apply(1, function(x) {
  120. ifelse(max(x) <= 0, NA, which.max(x))
  121. })
  122. work_simp$MAX <- work_simp %>% select(private:home) %>% as.matrix() %>%
  123. apply(1, function(x) {
  124. ifelse(max(x) <= 0, NA, which.max(x))
  125. })
  126. edu_from$MAX <- edu_from %>% select(drive:home) %>% as.matrix() %>%
  127. apply(1, function(x) {
  128. ifelse(max(x) <= 0, NA, which.max(x))
  129. })
  130. edu_to$MAX <- edu_to %>% select(drive:home) %>% as.matrix() %>%
  131. apply(1, function(x) {
  132. ifelse(max(x) <= 0, NA, which.max(x))
  133. })
  134. edu_simp$MAX <- edu_simp %>% select(drive:home) %>% as.matrix() %>%
  135. apply(1, function(x) {
  136. ifelse(max(x) <= 0, NA, which.max(x))
  137. })
  138. save(work_simp, work_to, work_from, edu_simp, edu_to, edu_from, tencols, file="viz/datasets.RData")