Skip to content

Commit

Permalink
Merge pull request #154 from SISBID/ava/finaltweaks
Browse files Browse the repository at this point in the history
A few final changes before Day 3
  • Loading branch information
avahoffman authored Jul 26, 2023
2 parents aca5676 + 69c6b70 commit c5e3d0b
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 141 deletions.
46 changes: 26 additions & 20 deletions lecture_notes/Functional_Programming.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ mtcars %>%
head(2)


## -------------------------------------------------------------------------------------------------------------

iris %>% filter(Sepal.Length > 2.4 & Sepal.Width > 2.4 &
Petal.Length > 2.4 & Petal.Width > 2.4)

iris %>% filter(if_all(Sepal.Length:Petal.Width, ~ . > 2.4))

iris %>% filter(if_all(where(is.numeric), ~ . > 2.4))


## -------------------------------------------------------------------------------------------------------------
mtcars %>%
filter(cyl > 3 & cyl < 8,
Expand All @@ -81,14 +71,30 @@ mtcars %>%


## -------------------------------------------------------------------------------------------------------------
x <-c(1.2, 2.3, 3.5, 4.6)
map(x, round) %>% unlist()

iris %>% filter(Sepal.Length > 2.4 & Sepal.Width > 2.4 &
Petal.Length > 2.4 & Petal.Width > 2.4)

iris %>% filter(if_all(Sepal.Length:Petal.Width, ~ . > 2.4))

iris %>% filter(if_all(where(is.numeric), ~ . > 2.4))


## -------------------------------------------------------------------------------------------------------------
vect <- c(1.2, 2.3, 3.5, 4.6)
map(vect, round) %>% unlist()


## -------------------------------------------------------------------------------------------------------------
my_tibble <- tibble(
values = c(1.2, 2.3, 3.5, 4.6)
)
map_df(my_tibble, round)


## -------------------------------------------------------------------------------------------------------------
x <-tibble(values = c(1.2, 2.3, 3.5, 4.6))
map_df(x, round)
modify(x, round)
modify(vect, round)
modify(my_tibble, round)


## -------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -135,7 +141,7 @@ system.time(iris %>%



## ----makeList, comment="", prompt=TRUE------------------------------------------------------------------------
## ----makeList-------------------------------------------------------------------------------------------------
mylist <- list(
letters = c("A", "b", "c"),
numbers = 1:3,
Expand All @@ -144,22 +150,22 @@ mylist <- list(
)


## ----Lists, comment="", prompt=TRUE---------------------------------------------------------------------------
## ----Lists----------------------------------------------------------------------------------------------------
head(mylist)


## ----Listsref1, comment="", prompt=TRUE-----------------------------------------------------------------------
## ----Listsref1------------------------------------------------------------------------------------------------
mylist[1] # returns a list
mylist["letters"] # returns a list


## ----Listsrefvec, comment="", prompt=TRUE---------------------------------------------------------------------
## ----Listsrefvec----------------------------------------------------------------------------------------------
mylist[[1]] # returns the vector 'letters'
mylist$letters # returns vector
mylist[["letters"]] # returns the vector 'letters'


## ----Listsref2, comment="", prompt=TRUE-----------------------------------------------------------------------
## ----Listsref2------------------------------------------------------------------------------------------------
mylist[1:2] # returns a list


Expand Down
74 changes: 40 additions & 34 deletions lecture_notes/Functional_Programming.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,9 @@ mtcars %>%

<!-- ``` -->

## `if_any()` and `if_all()` are also helpful!

Work inside other tidyverse functions, like `across()`

```{r}
iris %>% filter(Sepal.Length > 2.4 & Sepal.Width > 2.4 &
Petal.Length > 2.4 & Petal.Width > 2.4)
iris %>% filter(if_all(Sepal.Length:Petal.Width, ~ . > 2.4))
iris %>% filter(if_all(where(is.numeric), ~ . > 2.4))
```

<!-- iris %>% select(-Species) %>% filter(if_all(everything(), ~ . > 2.4)) -->

## Previously we filtered for patterns or conditions..

Seems a bit repetitive!
Dilemma: Seems a bit repetitive!

```{r}
mtcars %>%
Expand All @@ -180,19 +164,32 @@ mtcars %>%
carb > 3 & carb < 8)
```



## Now we can filter multiple columns!

Multiple conditions simultaneously!
`if_all()`: helps us filter on multiple similar conditions simultaneously!

```{r}
mtcars %>%
filter(if_all(c(cyl, gear, carb), ~.x > 3 & .x < 8))
```


## `if_any()` and `if_all()` are helpful!

Work inside other tidyverse functions, like `across()`

```{r}
iris %>% filter(Sepal.Length > 2.4 & Sepal.Width > 2.4 &
Petal.Length > 2.4 & Petal.Width > 2.4)
iris %>% filter(if_all(Sepal.Length:Petal.Width, ~ . > 2.4))
iris %>% filter(if_all(where(is.numeric), ~ . > 2.4))
```

<!-- iris %>% select(-Species) %>% filter(if_all(everything(), ~ . > 2.4)) -->

## `purrr` is also a super helpful package!

"Designed to make your functions purrr."
Expand Down Expand Up @@ -223,21 +220,30 @@ The `purrr` package can be very helpful!

the _* options specify the type of data output

## map (and modify)
## Using `map()`

![](https://dcl-prog.stanford.edu/images/map-step-2.png)
[[source](https://dcl-prog.stanford.edu/purrr-parallel.html)]

```{r}
x <-c(1.2, 2.3, 3.5, 4.6)
map(x, round) %>% unlist()
vect <- c(1.2, 2.3, 3.5, 4.6)
map(vect, round) %>% unlist()
```

## Using `map_df()`

```{r}
my_tibble <- tibble(
values = c(1.2, 2.3, 3.5, 4.6)
)
map_df(my_tibble, round)
```

## map (and modify)
## Using `modify()`

```{r}
x <-tibble(values = c(1.2, 2.3, 3.5, 4.6))
map_df(x, round)
modify(x, round)
modify(vect, round)
modify(my_tibble, round)
```

<!-- ## map2 -->
Expand Down Expand Up @@ -346,7 +352,7 @@ system.time(iris %>%
* Can hold vectors, strings, matrices, models, list of other lists, lists upon lists!
* Can reference data using $ (if the elements are named), or using [], or [[]]

```{r makeList, comment="", prompt=TRUE}
```{r makeList}
mylist <- list(
letters = c("A", "b", "c"),
numbers = 1:3,
Expand All @@ -357,20 +363,20 @@ mylist <- list(

## List Structure

```{r Lists, comment="", prompt=TRUE}
```{r Lists}
head(mylist)
```

## List referencing

```{r Listsref1, comment="", prompt=TRUE}
```{r Listsref1}
mylist[1] # returns a list
mylist["letters"] # returns a list
```

## List referencing

```{r Listsrefvec, comment="", prompt=TRUE}
```{r Listsrefvec}
mylist[[1]] # returns the vector 'letters'
mylist$letters # returns vector
mylist[["letters"]] # returns the vector 'letters'
Expand Down Expand Up @@ -403,7 +409,7 @@ mylist[["letters"]] # returns the vector 'letters'

You can also select multiple lists with the single brackets.

```{r Listsref2, comment="", prompt=TRUE}
```{r Listsref2}
mylist[1:2] # returns a list
```

Expand All @@ -427,7 +433,7 @@ str(mtcars %>% split(.$cyl))
```

## great example with split()
## great example with `split()`

```{r}
Expand Down
Loading

0 comments on commit c5e3d0b

Please sign in to comment.