Skip to content

Commit

Permalink
across
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rodrigues committed Mar 18, 2020
1 parent 1f40ad5 commit d058474
Show file tree
Hide file tree
Showing 96 changed files with 4,631 additions and 12,483 deletions.
61 changes: 29 additions & 32 deletions 04-descriptives.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -1687,35 +1687,12 @@ what I mean). It is also possible to summarise every column at once:
gasoline %>%
select(-year) %>%
group_by(country) %>%
summarise(across(everything, tibble::lst(mean, sd, min, max), .names = "{fn}_{col}"))
summarise(across(everything(), tibble::lst(mean, sd, min, max), .names = "{fn}_{col}"))
```
I removed the `year` variable because it's not a variable for which we want to have descriptive
statistics.
### mutate_*()
TBD
Of course, `mutate()` and `transmute()` also come with scoped versions:
```{r}
gasoline %>%
mutate_if(is.double, exp)
```
```{r}
gasoline %>%
mutate_at(vars(starts_with("l")), exp)
```
```{r}
gasoline %>%
mutate_all(as.character)
```
I think that by now, you are able to understand the above lines quite easily. If not, try some other
functions, or mutating other variables and you'll see that it is not that complicated!
## Other useful `{tidyverse}` functions
Expand Down Expand Up @@ -2897,6 +2874,29 @@ are the central objects and where the analyst operates over them with functional
### Exercise 1 {-}
* Combine `mutate()` with `across()` to exponentiate every column of type `double`.
```{r, eval = FALSE, include = FALSE}
gasoline %>%
mutate(across(is.double, exp))
```
* Exponeniate columns starting with the character `"l"`.
```{r, eval = FALSE, include = FALSE}
gasoline %>%
mutate(across(starts_with("l"), exp))
```
* Convert all columns' classes into the character class.
```{r, eval = FALSE, include = FALSE}
gasoline %>%
mutate(across(everything(), as.character))
```
### Exercise 2 {-}
Load the `LaborSupply` dataset from the `Ecdat` package and answer the following questions:
* Compute the average annual hours worked by year (plus standard deviation)
Expand Down Expand Up @@ -2949,31 +2949,28 @@ LaborSupply %>%
filter(year == 1980) %>%
group_by(no_kids) %>%
summarise(mean(lnwg), sd(lnwg), n())
```
### Exercise 2 {-}
### Exercise 3 {-}
* What does the following code do? Copy and paste it in an R interpreter to find out!
```{r, eval=FALSE}
LaborSupply %>%
group_by(id) %>%
mutate_at(vars(starts_with("l")), funs(lag, lead))
mutate(across(starts_with("l"), tibble::lst(lag, lead)))
```
`mutate_at()` is a scoped version of `mutate()` which allows you to specify a number of columns and
functions in one go. This also exists for `summarise()`.
* Using `summarise_at()`, compute the mean, standard deviation and number of individuals of `lnhr` and `lnwg` for each individual.
* Using `summarise()` and `across()`, compute the mean, standard deviation and number of individuals of `lnhr` and `lnwg` for each individual.
```{r, eval=FALSE, include = FALSE}
LaborSupply %>%
group_by(id) %>%
summarise_at(vars(starts_with("l"), funs(mean, sd)))
summarise(across(starts_with("l"), tibble::lst(mean, sd, n)))
```
### Exercise 3 {-}
### Exercise 4 {-}
* In the dataset folder you downloaded at the beginning of the chapter, there is a folder called
"unemployment". I used the data in the section about working with lists of datasets. Using
Expand Down
22 changes: 11 additions & 11 deletions 05-graphs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,31 @@ ggplot(starwars, aes(height)) +
geom_density()
```

Let's go into more detail now; what if you would like to plot the densities for females and males
Let's go into more detail now; what if you would like to plot the densities for feminines and masculines
only (removing the droids from the data first)? This can be done by first filtering the data using
`dplyr` and then separating the dataset by gender:


```{r, eval=FALSE}
starwars %>%
filter(gender %in% c("female", "male"))
filter(gender %in% c("feminine", "masculine"))
```

The above lines do the filtering; only keep gender if gender is in the vector `"female", "male"`.
This is much easier than having to write `gender == "female" | gender == "male"`. Then, we pipe
The above lines do the filtering; only keep gender if gender is in the vector `"feminine", "masculine"`.
This is much easier than having to write `gender == "feminine" | gender == "masculine"`. Then, we pipe
this dataset to `ggplot`:

```{r}
starwars %>%
filter(gender %in% c("female", "male")) %>%
filter(gender %in% c("feminine", "masculine")) %>%
ggplot(aes(height, fill = gender)) +
geom_density()
```

Let's take a closer look to the `aes()` function: I've added `fill = gender`. This means that
there will be one density plot for each gender in the data, and each will be coloured accordingly.
This is where `{ggplot2}` might be confusing; there is no need to write explicitly (even if it is
possible) that you want the *female* density to be red and the *male* density to be blue. You just
possible) that you want the *feminine* density to be red and the *masculine* density to be blue. You just
map the variable `gender` to this particular aesthetic. You conclude the plot by adding
`geom_density()` which is this case is the plot you want. We will see later how to change the
colours of your plot.
Expand All @@ -162,7 +162,7 @@ the aesthetics inside the `geom_density()` function:

```{r}
filtered_data <- starwars %>%
filter(gender %in% c("female", "male"))
filter(gender %in% c("feminine", "masculine"))
ggplot(filtered_data) +
geom_density(aes(height, fill = gender))
Expand Down Expand Up @@ -225,7 +225,7 @@ symbol in the code below:
starwars %>%
mutate(human = case_when(species == "Human" ~ "Human",
species != "Human" ~ "Not Human")) %>%
filter(gender %in% c("female", "male"), !is.na(human)) %>%
filter(gender %in% c("feminine", "masculine"), !is.na(human)) %>%
ggplot(aes(height, fill = gender)) +
facet_grid(. ~ human) + #<--- this is a formula
geom_density()
Expand All @@ -238,7 +238,7 @@ use it for facetting. By changing the formula, you change how the facetting is d
starwars %>%
mutate(human = case_when(species == "Human" ~ "Human",
species != "Human" ~ "Not Human")) %>%
filter(gender %in% c("female", "male"), !is.na(human)) %>%
filter(gender %in% c("feminine", "masculine"), !is.na(human)) %>%
ggplot(aes(height, fill = gender)) +
facet_grid(human ~ .) +
geom_density()
Expand All @@ -255,7 +255,7 @@ starwars %>%
n_films != 1 ~ "More than 1 movie")) %>%
mutate(human = case_when(species == "Human" ~ "Human",
species != "Human" ~ "Not Human")) %>%
filter(gender %in% c("female", "male"), !is.na(human)) %>%
filter(gender %in% c("feminine", "masculine"), !is.na(human)) %>%
ggplot(aes(height, fill = gender)) +
facet_grid(human ~ more_1) +
geom_density()
Expand Down Expand Up @@ -466,7 +466,7 @@ starwars %>%
geom_smooth(aes(height, mass, colour = gender))
```

Because there are only a few observations for females and `NA`s the regression lines are not very informative,
Because there are only a few observations for feminines and `NA`s the regression lines are not very informative,
but this was only an example to show you some options of `geom_smooth()`.

Let's go back to the unemployment line plots. For now, let's keep the base `{ggplot2}` theme, but
Expand Down
6 changes: 3 additions & 3 deletions 08-functional_programming.Rmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Functional programming
%>% # Functional programming

Functional programming is a paradigm that I find very suitable for data science. In functional
programming, your code is organised into functions that perform the operations you need. Your scripts
Expand Down Expand Up @@ -1349,7 +1349,7 @@ effects:

```{r}
simulation_results <- results %>%
mutate(difference = map2(.x = lpm, .y = true_effect, bind_cols)) %>%
mutate(difference = map2(.x = lpm, .y = true_effect, full_join)) %>%
mutate(difference = map(difference, ~mutate(., difference = true_effect - estimate))) %>%
mutate(difference = map(difference, ~select(., term, difference))) %>%
pull(difference) %>%
Expand Down Expand Up @@ -1383,7 +1383,7 @@ monte_carlo <- function(coefs, sample_size, repeats){
mutate(true_effect = map(true_effect, bind_rows))
simulation_results <- results %>%
mutate(difference = map2(.x = lpm, .y = true_effect, bind_cols)) %>%
mutate(difference = map2(.x = lpm, .y = true_effect, full_join)) %>%
mutate(difference = map(difference, ~mutate(., difference = true_effect - estimate))) %>%
mutate(difference = map(difference, ~select(., term, difference))) %>%
pull(difference) %>%
Expand Down
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-272-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-276-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-276-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-276-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-276-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-413-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-416-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-421-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-422-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-424-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-425-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-428-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-431-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-432-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-435-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-436-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-437-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-438-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-443-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-444-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-445-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-446-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-456-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-457-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-483-1.png
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-499-1.png
Loading

0 comments on commit d058474

Please sign in to comment.