From db778a4b46ca2436ac5856fde743d47fad9d117c Mon Sep 17 00:00:00 2001 From: Nics-Github Date: Wed, 8 Nov 2023 06:06:18 -0500 Subject: [PATCH] day 26 --- .../lectures/Day_26_across_maps.qmd | 97 ++ .../lectures/Day_26_across_maps.html | 954 ++++++++++++++++++ 2 files changed, 1051 insertions(+) create mode 100644 course-materials/lectures/Day_26_across_maps.qmd create mode 100644 docs/course-materials/lectures/Day_26_across_maps.html diff --git a/course-materials/lectures/Day_26_across_maps.qmd b/course-materials/lectures/Day_26_across_maps.qmd new file mode 100644 index 0000000..4961dea --- /dev/null +++ b/course-materials/lectures/Day_26_across_maps.qmd @@ -0,0 +1,97 @@ +--- +title: "iteration" +author: "Schwab" +format: revealjs +editor: visual +execute: + echo: true +--- + +## `across()` and `map()` + +```{r} +#| include: false + + +library(tidyverse) +``` + +These functions allow us to preform the same operation across multiple rows. + +`map()` comes from the `purr` package. + +`across()` comes from the `dplyr` package. + +## Example: `iris` + +```{r} +head(iris) +``` + +## Average + +Let's find the average of each column that ends with `.Length` by species. + +```{r} +iris %>% + group_by(Species) %>% + summarise( + across(.cols = ends_with("Length"), + .fns = list(mean = mean))) +``` + +## Rounding with `across()` + +```{r} +iris %>% + group_by(Species) %>% + summarise(across( + .cols = Sepal.Length:Petal.Width, + .fns = round)) +``` + +## Rounding without `across()` + +```{r} +iris %>% + group_by(Species) %>% + summarise(Sepal.Length = round(Sepal.Length), + Sepal.Width = round(Sepal.Width), + Petal.Length = round(Petal.Length), + Petal.Width = round(Petal.Width)) +``` + +## Rounding + +```{r} +iris %>% + group_by(Species) %>% + reframe(across( + .cols = starts_with("Sepal"), + .fns = ~ round(. , digits = 2))) +``` + +## `map()` is similar to `across()` + +It performs some operation on a data frame, vector or list. + +```{r} + +iris |> + map(.f = mean) +``` + +## Different maps() + +`map()` returns a list. + +`map_dfc()` returns a dataframe with columns + +`map_dfr()` returns a dataframe with rows. + +## `map_dfc()` + +```{r} +iris |> + map_dfc(.f = mean) +``` diff --git a/docs/course-materials/lectures/Day_26_across_maps.html b/docs/course-materials/lectures/Day_26_across_maps.html new file mode 100644 index 0000000..804f73c --- /dev/null +++ b/docs/course-materials/lectures/Day_26_across_maps.html @@ -0,0 +1,954 @@ + + + + + + + + + + + + + + SDS 192 Fall ’23 - iteration + + + + + + + + + + + + + + + +
+
+ +
+

iteration

+ +
+
+
+Schwab +
+
+
+ +
+
+

across() and map()

+

These functions allow us to preform the same operation across multiple rows.

+

map() comes from the purr package.

+

across() comes from the dplyr package.

+
+
+

Example: iris

+
+
head(iris)
+
+
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
+1          5.1         3.5          1.4         0.2  setosa
+2          4.9         3.0          1.4         0.2  setosa
+3          4.7         3.2          1.3         0.2  setosa
+4          4.6         3.1          1.5         0.2  setosa
+5          5.0         3.6          1.4         0.2  setosa
+6          5.4         3.9          1.7         0.4  setosa
+
+
+
+
+

Average

+

Let’s find the average of each column that ends with .Length by species.

+
+
iris %>%
+  group_by(Species) %>%
+  summarise(
+    across(.cols = ends_with("Length"), 
+           .fns = list(mean = mean)))
+
+
# A tibble: 3 × 3
+  Species    Sepal.Length_mean Petal.Length_mean
+  <fct>                  <dbl>             <dbl>
+1 setosa                  5.01              1.46
+2 versicolor              5.94              4.26
+3 virginica               6.59              5.55
+
+
+
+
+

Rounding with across()

+
+
iris %>%
+  group_by(Species) %>%
+  summarise(across(
+    .cols = Sepal.Length:Petal.Width, 
+    .fns = round))
+
+
# A tibble: 150 × 5
+# Groups:   Species [3]
+   Species Sepal.Length Sepal.Width Petal.Length Petal.Width
+   <fct>          <dbl>       <dbl>        <dbl>       <dbl>
+ 1 setosa             5           4            1           0
+ 2 setosa             5           3            1           0
+ 3 setosa             5           3            1           0
+ 4 setosa             5           3            2           0
+ 5 setosa             5           4            1           0
+ 6 setosa             5           4            2           0
+ 7 setosa             5           3            1           0
+ 8 setosa             5           3            2           0
+ 9 setosa             4           3            1           0
+10 setosa             5           3            2           0
+# ℹ 140 more rows
+
+
+
+
+

Rounding without across()

+
+
iris %>%
+  group_by(Species) %>%
+  summarise(Sepal.Length = round(Sepal.Length),
+            Sepal.Width = round(Sepal.Width),
+            Petal.Length = round(Petal.Length),
+            Petal.Width = round(Petal.Width))
+
+
# A tibble: 150 × 5
+# Groups:   Species [3]
+   Species Sepal.Length Sepal.Width Petal.Length Petal.Width
+   <fct>          <dbl>       <dbl>        <dbl>       <dbl>
+ 1 setosa             5           4            1           0
+ 2 setosa             5           3            1           0
+ 3 setosa             5           3            1           0
+ 4 setosa             5           3            2           0
+ 5 setosa             5           4            1           0
+ 6 setosa             5           4            2           0
+ 7 setosa             5           3            1           0
+ 8 setosa             5           3            2           0
+ 9 setosa             4           3            1           0
+10 setosa             5           3            2           0
+# ℹ 140 more rows
+
+
+
+
+

Rounding

+
+
iris %>%
+  group_by(Species) %>%
+  reframe(across(
+    .cols = starts_with("Sepal"), 
+    .fns = ~ round(. , digits = 2)))
+
+
# A tibble: 150 × 3
+   Species Sepal.Length Sepal.Width
+   <fct>          <dbl>       <dbl>
+ 1 setosa           5.1         3.5
+ 2 setosa           4.9         3  
+ 3 setosa           4.7         3.2
+ 4 setosa           4.6         3.1
+ 5 setosa           5           3.6
+ 6 setosa           5.4         3.9
+ 7 setosa           4.6         3.4
+ 8 setosa           5           3.4
+ 9 setosa           4.4         2.9
+10 setosa           4.9         3.1
+# ℹ 140 more rows
+
+
+
+
+

map() is similar to across()

+

It performs some operation on a data frame, vector or list.

+
+
iris |>
+  map(.f = mean)
+
+
$Sepal.Length
+[1] 5.843333
+
+$Sepal.Width
+[1] 3.057333
+
+$Petal.Length
+[1] 3.758
+
+$Petal.Width
+[1] 1.199333
+
+$Species
+[1] NA
+
+
+
+
+

Different maps()

+

map() returns a list.

+

map_dfc() returns a dataframe with columns

+

map_dfr() returns a dataframe with rows.

+
+
+

map_dfc()

+
+
iris |>
+  map_dfc(.f = mean)
+
+
# A tibble: 1 × 5
+  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
+         <dbl>       <dbl>        <dbl>       <dbl>   <dbl>
+1         5.84        3.06         3.76        1.20      NA
+
+
+ + +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file