> For the complete documentation index, see [llms.txt](https://concordia-infant-research-lab.gitbook.io/lab-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://concordia-infant-research-lab.gitbook.io/lab-wiki/project-workflow-data-analysis/getting-started-with-r/coding-in-the-tidyverse.md).

# Coding in the tidyverse

Best practices for human-readable code

The [tidyverse](https://www.tidyverse.org/) is a collection of packages designed by [Hadley Wickham](https://hadley.nz/). There are many different ways to code in R, including base R, but this lab uses the tidyverse wherever possible for a few good reasons:

* Tidyverse functions are pretty human-readable which makes troubleshooting a lot easier
* Other lab members will understand your code better if everyone commits to the same coding style
* There are a ton of resources online to help you understand tidyverse functions
* Tidyverse functions expect tidy data, which means that they force you to use good data management practices
* The functions in the tidyverse are very powerful and often are designed to facilitate exactly the kinds of transformations we need.

### Installing and loading the tidyverse

If this is your first time using the tidyverse, you'll need to install all of the packages. Luckily they all come bundled together and can be installed with one line of code! Simply write&#x20;

```
install.packages("tidyverse")
```

into the console of your Rstudio session. Be sure to watch for any additional prompts along the way as you install!

After you've installed the tidyverse, you'll need to load it. Think of installing as screwing in a lightbulb and loading as flipping the light switch. You only need to screw it in once to use the lightbulb, but you need to flip the switch every time. To load the packages, you should add

```
library(tidyverse)
```

at the beginning of each script where you use tidyverse functions.&#x20;

### Main tidyverse functions

**The pipe:** `%>%` this can be read as "and then" whenever you encounter it in code. \
`data %>%` Take the dataframe "data" and then\
`group_by(id) %>%` group the data by the column `id` and then\
`distinct(media_name)`keep only one row (per group) with each distinct `media_name`

[**Select**](https://dplyr.tidyverse.org/reference/select.html) allows you to remove columns in your dataframe, or move columns around into a better order

[**Filter**](https://dplyr.tidyverse.org/reference/filter.html) allows you to remove rows that don't match some criteria you set out. Great for cleaning data.

[**Mutate**](https://dplyr.tidyverse.org/reference/mutate.html) allows you to make a new column using previous columns

[**Pivot longer**](https://tidyr.tidyverse.org/reference/pivot_longer.html) allows you to transpose wide data (e.g. qualtrics output) into a longer format

[**pivot wider**](https://tidyr.tidyverse.org/reference/pivot_wider.html) is the reverse of pivot longer, it allows you to transpose long data into a wider format.

### Programming with dplyr

**dplyr** is a gramar of data manipulation and wrangling that provides a series of consistent verbs within the tydyverse.&#x20;

**Helper functions** that allow to more precisely select columns when using the **select** function:

<mark style="color:blue;">**Starts\_with**</mark> allows you to select all the columns of a data frame that start with a specific substring. For example "per" to select all the columns' names that start with the word "percentage".

<mark style="color:blue;">**Ends\_with**</mark> allows you to select all the columns of a data frame that end with a specific substring. For example "ratio" to select all the columns' names that end with the word "ratio".

<mark style="color:blue;">**Contains**</mark> allows you to select all the columns of a data frame that contain a specific substring anywhere in their name. For example "199" to select all the columns' names that contain dates from the 90's.&#x20;

<mark style="color:blue;">**Matches**</mark> allows you to select all the columns of a data frame that match several criteria. This function works with regular expressions (see below). For example `"y|perc"` to select all the columns' names that either contain the expression "y" or the expression "perc". &#x20;

**Helper functions** that allow to modify more efficiently columns when using the **mutate** function:

<mark style="color:blue;">**Across**</mark> allows you to perform the same calculations across multiple rows when using the mutate function.&#x20;

<mark style="color:blue;">**Sub**</mark> allows you to replace the first occurrence of a substring with a new pattern.

<mark style="color:blue;">**Gsub**</mark> allows you to replace all the occurrences of a substring with a new pattern.

<mark style="color:blue;">**Where**</mark> allows you to more efficiently specify columns for a calculation or a replacement. For example `across(.cols=where(is.numeric()))`

**Helper functions** that allow a more efficient selection of columns when using the **filter** function:

<mark style="color:blue;">**If\_any**</mark> allows you to specify rows to be filtered based on the matching of specified criteria. For example : `filter(if_any(.cols= starts_with("perc")))`

<mark style="color:blue;">**If\_all**</mark> works similarly to the if\_any function, but it is used when multiple rows match the specified criteria.

**Helper functions** that allow combining datasets:

<mark style="color:blue;">**Left\_join**</mark> allows you to keep all the rows from the dataset on the left plus the rows in common from the right data frame. The columns by which the dataframes are joined must have the same names in the left and right dtaaframes.

<mark style="color:blue;">**Inner\_join**</mark> allows you to keep only the rows that are in common between two datasets.

<mark style="color:blue;">**Anti\_join**</mark> allows you to identify rows that are present in one dataset, and that are not present in the second one.&#x20;

<figure><img src="https://3390241049-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FG4X3PM9X7Cp5gi9bcjVG%2Fuploads%2FdGxd7PsuYeI80q8hrday%2Fjoin_function_diagrams.png?alt=media&amp;token=a14eedab-a281-40e9-b3f6-1fdedd37291e" alt=""><figcaption></figcaption></figure>

### Regular expressions in the tidyverse

**Regular expressions** are tools for describing patterns in strings. They work with the **stringr** library within the tidyverse.&#x20;

<mark style="color:blue;">**Alternation**</mark> use the token `|` when specifiying an "or" parameter. For example `"green|blue"` to specify strings that contain either the expression green or the expression blue.&#x20;

<mark style="color:blue;">**Anchors**</mark> use the token `^` when searching for a match at the start of the string (similar to starts\_with). For example `"^co"`. Use the token `$` when searching for a match at the end of a string (similar to ends\_with). For example `"co$"`.

### Set theory clauses

**Set theory clauses** are useful functions to join multiple datasets together when working in the tidyverse.

<mark style="color:blue;">**Intersect**</mark> only keeps rows that exist in both datasets.

<mark style="color:blue;">**Union**</mark> keeps all the rows from both datasets without duplicating the repeated rows.

<mark style="color:blue;">**Union\_all**</mark> keeps all the rows from both datasets duplicating all the repeated rows.

<mark style="color:blue;">**Setdiff**</mark> keeps all the rows in the x dataset that are different from the rows in the y dataset.

<figure><img src="https://3390241049-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FG4X3PM9X7Cp5gi9bcjVG%2Fuploads%2F6WA3O9MDvYVVv2Bm7yIt%2Fset_theory_clauses.png?alt=media&amp;token=249edf8f-d38c-4392-bc2d-0723ddafb227" alt=""><figcaption></figcaption></figure>
