]> code.communitydata.science - stats_class_2020.git/blobdiff - psets/pset3-worked_solution.rmd
typos for ch5 and more elaborate tidyverse example code w more verbose
[stats_class_2020.git] / psets / pset3-worked_solution.rmd
index e4b6d32985e421c6b90e7d5890953ffc4dddd8ce..f99c666c989f45757448c22cfd9f6bd358087000 100644 (file)
@@ -176,26 +176,65 @@ round(
 ```
 Here the noteworthy comparisons again arise within the black and hispanic categories. Both groups account for a substantially larger proportion of stops resulting in searches vs. those that do not result in searches. 
 
-For the sake of completeness/comparison, here's a way to do similar cross-tabulations in single a chunk of tidyverse code. I include conditional proportions of all stops to facilitate comparison with some of the tables I created earlier as well:  
-```{r tidyverse crosstabs}
+For the sake of completeness/comparison, here's a way to do similar cross-tabulations in chunks of tidyverse code. This first bit summarizes the number of stops and proportion of total stops accounted for within each of the categories of `subject_race`.
+```{r tidyverse stops by subject_race}
 ilstops %>%
   group_by(subject_race) %>%
   filter(!is.na(subject_race)) %>%
   summarize(
     n_stops = n(),
     prop_total_stops = round(n() / nrow(ilstops), digits=3),
+    )
+```
+In that block I first make a call to `group_by()` to tell R that I want it to run subsequent commands on the data "grouped" within the categories of `subject_race`. Then I pipe the grouped data to `summarize()`, which I use to calculate the number of stops within each group (in this data that's just the number of observations within each group) as well as the proportion of total stops within each group.  
+
+What about counting up the number and proportion of searches within each group? One way to think about that is as another call to `summarize()` (since, after all, I want to calculate the summary information for searches within the same groups). Within the Tidyverse approach to things, this kind of summarizing within groups and within another variable (`search_conducted` in this case) can be accomplished with the `across()` function. 
+
+In general, the `across()` function seems to usually be made within a call to another verb like `summarize()` or `mutate()`. The syntax for `across()` is similar to these others. It requires two things: (1) at least one variable to summarize across (`search_conducted` here) and (2) the outputs I want.
+
+In this particular case, I'll use it to calculate the within group sums of `search_conducted`. Notice that I also filter out the missing values from `search_conducted` before I call `summarize` here.
+```{r }
+ilstops %>%
+  group_by(subject_race) %>%
+  filter(!is.na(subject_race), !is.na(search_conducted)) %>%
+  summarize(
+    across(search_conducted, sum)
+    )
+```
+If I want `across()` to calculate more than one summary, I need to provide it a list of things (in a `name = value` format sort of similar to `summarize()` or `mutate()`).  
+
+```{r}
+ilstops %>%
+  group_by(subject_race) %>%
+  filter(!is.na(subject_race) & !is.na(search_conducted)) %>%
+  summarize(
     across(
       search_conducted,
       list(
-        sum = ~ sum(.x, na.rm=TRUE),
-        over_n_stops = ~ round(mean(.x, na.rm=TRUE), digits=3)  
+        sum = sum,
+        over_n_stops = mean
         )
       )
-    ) %>%
-      arrange(desc(n_stops))
+    )
 ```
+I can clean this up a bit by using two functions to the output in descending order by one of the columns. I do this with a nested call to two functions `arrange()` and `desc()`. I can also insert my earlier summary statistics for the number and proportions of stops by group back into the table.
 
-Notice the use of `across()` within a call to `summarize()` provides one way to calculate conditional summariy info. I also use a nested call to `arrange()` and `desc()` at the end to sort my results in descending order by one of the columns.  
+```{r}
+ilstops %>%
+  group_by(subject_race) %>%
+  filter(!is.na(subject_race) & !is.na(search_conducted)) %>%
+  summarize(
+    n_stops = n(),
+    prop_total_stops = round(n() / nrow(ilstops), digits=3),
+    across(
+      search_conducted,
+      list(
+        sum = sum,
+        over_n_stops = mean)  
+        )
+      ) %>%
+      arrange(desc(n_stops))
+```
 
 ### Searches by `date`
 

Community Data Science Collective || Want to submit a patch?