DS4PS / course_website

https://ds4ps.github.io/course_website/
0 stars 0 forks source link

Lab 11 - Can't get Q1 to run; Coding error?! #23

Open jcheung2 opened 5 years ago

jcheung2 commented 5 years ago

This is Q1. What am I NOT doing correctly.

d2 <-
  dat %>%
  mutate (drv.ped = (Unittype_One == c("Pedalcyclist", "Pedestrian") | Unittype_Two == c ("Pedalcyclist", "Pedestrian")),
          drv.drv = (Unittype_One == "Driver" & Unittype_Two == "Driver"))

count(d2, drv.ped, drv.drv)

tot.drv.ped <- sum (drv.ped=T)

qmplot( Longitude, Latitude, data=d2, geom = "blank", 
  zoom = 13, maptype = "toner-background", darken = .7 ) +
  stat_density_2d( aes(fill = ..level..), geom = "polygon", alpha=0.3, color = NA) +
  scale_fill_gradient2( low="white", mid="yellow", high="red",  midpoint=200 ) + 
  facet_wrap( ~ tot.drv.ped, nrow=1, ncol=4 )
lecy commented 5 years ago

Long answer short - change:

facet_wrap( ~ tot.drv.ped )

To:

facet_wrap( ~ drv.ped )

You need a group variable here - a vector which is the same length as nrow() in the dataset. Your tot.drv.ped is a single value, so you can't 'facet' (split the data into groups) based upon that.

library( dplyr )
library( ggmap )

URL <- "https://github.com/DS4PS/Data-Science-Class/blob/master/DATA/TempeTrafficAccidents.rds?raw=true"
dat <- readRDS(gzcon(url( URL )))

d2 <-
  dat %>%
  mutate (drv.ped = (Unittype_One %in% c("Pedalcyclist", "Pedestrian") | Unittype_Two %in% c ("Pedalcyclist", "Pedestrian")),
          drv.drv = (Unittype_One %in% "Driver" & Unittype_Two %in% "Driver"))

qmplot( Longitude, Latitude, data=d2, geom = "blank", 
  zoom = 13, maptype = "toner-background", darken = .7 ) +
  stat_density_2d( aes(fill = ..level..), geom = "polygon", alpha=0.3, color = NA) +
  scale_fill_gradient2( low="white", mid="yellow", high="red",  midpoint=200 ) + 
  facet_wrap( ~ drv.ped, nrow=1, ncol=4 )

image

lecy commented 5 years ago

Please note this subtle but very important difference in how you are defining groups:

d2 <-
  dat %>%
  mutate (drv.ped = (Unittype_One == c("Pedalcyclist", "Pedestrian") | Unittype_Two == c ("Pedalcyclist", "Pedestrian")),
          drv.drv = (Unittype_One == "Driver" & Unittype_Two == "Driver"))

count( d2, drv.ped, drv.drv )

#    A tibble: 3 x 3
#   drv.ped drv.drv     n
#   <lgl>   <lgl>   <int>
# 1 FALSE   FALSE    3209
# 2 FALSE   TRUE    24514
# 3 TRUE    FALSE     747

VERSUS:

d2 <-
  dat %>%
  mutate (drv.ped = (Unittype_One %in% c("Pedalcyclist", "Pedestrian") | Unittype_Two %in% c ("Pedalcyclist", "Pedestrian")),
          drv.drv = (Unittype_One %in% "Driver" & Unittype_Two %in% "Driver"))

count( d2, drv.ped, drv.drv )

#   A tibble: 3 x 3
#   drv.ped drv.drv     n
#   <lgl>   <lgl>   <int>
# 1 FALSE   FALSE    2437
# 2 FALSE   TRUE    24514
# 3 TRUE    FALSE    1519

When you are writing compound logical statements remember that you either need an OR operator, or the %in% operator.

CORRECT:

x == "A" | x == "B"  # technically correct way to write the statement
x %in% c("A","B")     # shortcut for the line above

INCORRECT:

x == c("A","B")
jcheung2 commented 5 years ago

It still won't run. I completely closed down RStudio, reopened the program and .rmd file. S

Cut and pasted your updated code into my document and STILL won't run.

I've reloaded the packages several times.

lecy commented 5 years ago

Your code runs fine on my computer, so it's a package issue. Try shutting down R Studio, opening an R console, and re-installing ggmap:

devtools::install_github("dkahle/ggmap")
devtools::install_github("hadley/ggplot2")