```{r}
#| include: false

library(webexercises)
set.seed(32986)
```

## Some basics

In the tutorial last week you've been playing around with R and variables.

**But what if you want to import data in R?**

Easy! You can use the `read_*()` functions to read your files into R. But before we dive in, let's first talk about some computer basics. (You can skip this section if it's too basic for you)

### Files, folder and file extensions

Files saved on your computer live in a specific place. For example, if you download a file from a browser (like Google Chrome, Safari or Firefox), the file is normally saved in the `Download` folder.

But where does the `Download` folder live? Usually, in your user folder! The user folder normally is the name of your account or a name you picked when you created your computer account. In my case, my user folder is simply called `ste`.

::: callout-tip
#### User folder

The **user folder** is the folder with the name of your account.
:::

::: {.callout-note collapse="true"}
#### How to find your user folder name

**On macOS**

-   Open the Finder Preferences.

-   Go to `Sidebar`.

-   The name next to the house icon is the name of your home folder.

**On Windows**

-   Right-click an empty area on the navigation panel in File Explorer.

-   From the context menu, select the 'Show all folders' and your user profile will be added as a location in the navigation bar.
:::

So, let's assume I download a file, let's say `big_data.csv`, in the `Download` folder of my user folder.

Now we can represent the location of the `big_data.csv` file like so:

```         
ste/
└── Downloads/
    └── big_data.csv
```

To mark that `ste` and `Downloads` are folders, we add a final forward slash `/`. That simply means "hey! I am a folder!". `big_data.csv` is a file, so it doesn't have a final `/`.

Instead, the file name `big_data.csv` has a **file extension**. The file extension is `.csv`. A file extension marks the type of file: in this the `big_data` file is a `.csv` file, a comma separated value file (we will see an example of what that looks like later).

Different file type have different file extensions:

-   Excel files: `.xlsx`.
-   Plain text files: `.txt`.
-   Images: `.png`, `.jpg`, `.gif`.
-   Audio: `.mp3`, `.wav`.
-   Video: `.mp4`, `.mov`, `.avi`.
-   Etc...

::: callout-tip
#### File extension

A file extension is a sequence of letters that indicates the type of a file and it's separated with a `.` from the file name.
:::

### File paths

Now, we can use an alternative, more succinct way, to represent the location of the `big_data.csv`:

`ste/Downloads/big_data.csv`

This is called a **file path**! It's the path through folders that lead you to the file. Folders are separated by `/` and the file is marked with the extension `.csv`.

::: callout-tip
#### File path

A **file path** indicates the location of a file on a computer as a path through folders that lead you to the file.
:::

Now the million pound question: where does `ste/` live on my computer???

User folders are located in different places depending on the operating system you are using:

-   On **macOS**: the user folder is in `/Users/`.

    -   You will notice that there is a forward slash also before the name of the folder. That is because the `/Users/` folder is a top folder, i.e. there are no folders further up in the hierarchy of folders.
    -   This means that the full path for the `big_data.csv` file on a computer running macOS would be: `/Users/ste/Downloads/big_data.csv`.

-   On **Windows**: the user folder is in `C:/Users/`

    -   You will notice that `C` is followed by a colon `:`. That is because `C` is a drive, which contains files and folders. `C:` is not contained by any other folder, i.e. there are no other folders above `C:` in the hierarchy of folders.
    -   This means that the full path for the `big_data.csv` file on a Windows computer would be: `C:/Users/ste/Downloads/big_data.csv`.

When a file path starts from a top-most folder, we call that path the **absolute** file path.

::: callout-tip
#### Absolute path

An **absolute path** is a file path that starts with a top-most folder.
:::

There is another type of file paths, called **relative** paths. A relative path is a partial file path, relative to a specific folder. You will learn how to use relative paths below, when we will go through importing files in R using R scripts below.

Importing files in R is very easy with the tidyverse packages. You just need to know the file type (very often the file extension helps) and the location of the file (i.e. the file path).

The next sections will teach you how to import data in R!

::: callout-note
#### Quiz 1

```{r}
#| label: quiz-1
#| results: asis
#| echo: false

opts_1 <- c(
   "`Downloads/courses/qml/data/`",
   answer = "`/Users/smruti/Downloads/data.csv/files/`",
   "`sascha/Documents/files_pdf/paper.pdf`"
)

cat("Which of the following paths is an impossible path?", longmcq(opts_1))
```
:::

## The data

For this tutorial, we will use the data from this paper: Song et al. 2020. *Second language users exhibit shallow morphological processing*. DOI: [10.1017/S0272263120000170](https://doi.org/10.1017/S0272263120000170).

The study consisted of a lexical decision task in which participants where first shown a prime, followed by a target word for which they had to indicate whether it was a real word or a nonce word.

The prime word belonged to one of three possible groups (`Relation_type` in the data) each of which refers to the morphological relation of the prime and the target word:

-   `Unrelated`: for example, *prolong* (assuming *unkindness* as target, \[\[un-kind\]-ness\]).

-   `Constituent`: *unkind*.

-   `NonConstituent`: *kindness*.

The expectations are that

1.  Lexical decisions for native English participants should be facilitated in the `Constituent` condition, but not in the `Unrelated` and `NonConstituent` conditions.
2.  Lexical decisions for non-native English participants should be facilitated both in the `Constituent` and `NonCostituent` condition, but not in the `Unrelated` condition.

We can interpret "facilitated lexical decisions" as higher accuracy and shorter reaction times.

In section 2-4 of this tutorial you will learn about:

-   The data file format (a `.csv file`).

-   How to read data with the `read_csv()` function from the tidyverse packages and what packages are.

-   How to view the imported data.

### Download the data file

Please, follow these instructions carefully.

1.  Download the file with the data by right-clicking on the following link and download the file: [shallow.csv](../data/shallow.csv). (Note that tutorial files are also linked in the [Course content](../content.qmd) page)

2.  Create a folder called `data/` (the slash is there just to remind you that it's a folder, but you don't have to include it in the name) in the RStudio project of the course.

    1.  To create a folder, go to the Files tab of the bottom-right panel in RStudio.

    2.  Make sure you are viewing the project's main folder.

    3.  Click on the `New Folder` button, enter "data" in the text box and click `OK`

3.  Move the downloaded file into the `data/` folder.

    1.  Open a Finder or File Explorer window.

    2.  Navigate to the folder where you have saved the downloaded file (it will very likely be the `Downloads/` folder).

    3.  Copy the file.

    4.  In Finder or File Explorer, navigate to the RStudio project folder, then the `data/` folder, and paste the file in there.

The rest of the tutorial will assume that you have created a folder called `data/` in the RStudio project folder and that `shallow.csv` is in that folder.

I recommend you to start being very organised with your files from now, whether it's for this course or your dissertation or else. I also suggest to avoid overly nested structures (for example, avoid having one folder for each week for this course. Rather, save all data files in the `data/` folder).

::: {.callout-note collapse="true"}
#### Organising your files

The Open Science Framework has the following recommendations that apply very well to any type of research project.

-   Use **one folder** per project. This will also be your RStudio project folder.

-   Separate **raw data** from **derived data**.

-   Separate **code** from **data**.

-   Make **raw data read-only**.

To learn more about this, check the OSF page [Organising files](https://help.osf.io/article/147-organizing-files).

In brief, what these recommendations mean is that you want a folder for your research project/course/else, and inside the folder two folders: one for data and one for code.

The `data/` folder could further contain `raw/` for raw data (data that should not be lost or changed, for example collected data or annotations) and `derived/` for data that derives from the raw data, for example through automated data processing.

I usually also have a separate folder called `figs/` or `img/` where I save plots. Of course which folders you will have it's ultimately up to you and needs will vary depending on the project and field!
:::

### About `shallow.csv`

`shallow.csv` is a `.csv` file. It looks like this (the following is just an excerpt from the file, specifically the first 5 lines):

``` txt
Group,ID,List,Target,ACC,RT,logRT,Critical_Filler,Word_Nonword,Relation_type,Branching
L1,L1_01,A,banoshment,1,423,6.0474,Filler,Nonword,Phonological,NA
L1,L1_01,A,unawareness,1,603,6.4019,Critical,Word,Unrelated,Left
L1,L1_01,A,unholiness,1,739,6.6053,Critical,Word,Constituent,Left
L1,L1_01,A,bictimize,1,510,6.2344,Filler,Nonword,Phonological,NA
```

A `.csv` file is basically a format to save tabular data (i.e. data that looks like a table). To separate each column, a `.csv` file uses a comma `,` (hence the name "comma separated values").

The first line of the file indicates the names of the columns of the table:

``` txt
Group,ID,List,Target,ACC,RT,logRT,Critical_Filler,Word_Nonword,Relation_type,Branching
```

There are 11 columns.

Then, the rest of the file contains the other rows of the table, with values for each of the 11 columns. Of course, separated by commas.

``` txt
L1,L1_01,A,banoshment,1,423,6.0474,Filler,Nonword,Phonological,NA
L1,L1_01,A,unawareness,1,603,6.4019,Critical,Word,Unrelated,Left
L1,L1_01,A,unholiness,1,739,6.6053,Critical,Word,Constituent,Left
L1,L1_01,A,bictimize,1,510,6.2344,Filler,Nonword,Phonological,NA
```

The `shallow.csv` file should be placed in the `data/` folder of the course RStudio project.

Now, let's import the data!

Importing `.csv` files is very easy. You can use the `read_csv()` function from a collection of R packages known as the [tidyverse](https://www.tidyverse.org).

But what are R packages? Find out in the following section.

## R packages

When you install R, a **library** of packages is also installed. **Packages** are like plug-ins, they provide R with extra functionalities. The default **R library** contains all of the base R packages.

You can check all of the currently installed packages in the bottom-right panel of RStudio, in the **Packages** tab.

If you want to find the path of the R library on your computer, type `.libPaths()` in the Console. The function returns (i.e. outputs) the path or paths where your R library is.

::: callout-tip
#### R library and packages

-   The **R library** contains the base R packages and all user-installed packages.

-   **R packages** provide R with extra functionalities and are installed into the R library.
:::

#### Install packages

You can install extra packages in the R library in two ways:

1.  You can use the `install.packages()` function. This function takes the name of package you want to install as a string, for example `install.packages("cowsay")`
2.  Or you can go the `Packages` tab in the bottom-right panel of RStudio and click on `Install`.

Go ahead and try to install a package using the first method. Install the fortunes package (remember to quote the name of the package). After installing you will see that the package fortunes is listed in the Packages tab.

::: callout-warning
**You need to install a package ONLY ONCE!** Once installed, it's there for ever, saved in the R library. You will be able to use all of your installed packages in any RStudio project you create.
:::

#### Attach packages

Now, to use a package you need to **attach** the package with the `library()` function.

::: callout-warning
**You need to attach the package you want to use once per R session.**
:::

Let's attach the fortunes package. Run the following code in the Console. Note that while `install.packages("fortunes")` takes the name of the package as a string, `library(fortunes)` takes the name of the package without quotes.

```{r fortunes}
library(fortunes)
```

Now you can use the functions provided by the attached package. Try it out!

```{r fortune}
fortune()
```

To learn what a function does, you can check its documentation by typing the function name preceded by a `?` question mark. Type `?function` in the Console and hit ENTER to see the function documentation.

::: callout-warning
Remember, you need to install a package only once but you need to attach it with `library()` every time you start R.

Think of `install.packages()` as mounting a light bulb (installing the package) and `library()` as the light switch (attaching the package).

![](/img/install-vs-attach.jpeg)
:::

::: callout-note
#### Quiz 2

```{r}
#| label: quiz-2
#| results: asis
#| echo: false

opts_2 <- c(
   answer = "You attach libraries with `library()`.",
   "`install.packages()` does not load packages.",
   "The R library is a folder."
)

cat("Which of the following statements is wrong?", longmcq(opts_2))
```
:::

## Import and view data

### Import the data

To import data in R we will use the `read_csv()` function from the [readr](https://readr.tidyverse.org) package, one of the tidyverse packages.

The `read_csv()` function only requires you to specify the file path as a string (remember, strings are quoted between `" "`, for example `"year_data.txt"`).

On my computer, the file path of `shallow.csv` is `/Users/ste/qml/data/shallow.csv`, but on your computer the file path will be different, of course.

Also, note that it is not enough to use the `read_csv()` function. You also must assign the output of the `read_csv()` function (i.e. the data we are reading) to a variable, using the assignment arrow `<-`.

And since the `read_csv()` is a function from the tidyverse, you first need to attach the tidyverse packages with `library(tidyverse)` (remember, you need to attach packages **only once** per session).

This will attach a set of the tidyverse packages that are commonly used in most data analysis applications, including readr. Of course, you can also attach the individual packages directly: `library(readr)`. If you use `library(tidyverse)` there is no need to attach individual tidyverse packages.

So, putting all together, here's the code. **Remember to change the file path to match the path of the file on your computer**. Run the code in the Console to read the data.

```{r}
#| label: read-example
#| eval: false
library(tidyverse)

# CHANGE THE FILE PATH TO MATCH THE PATH ON YOUR COMPUTER
shallow <- read_csv("/Users/ste/qml/data/shallow.csv")
```

Fantastic!

### View the data

Now we can view the data.

The easiest way is to click on the name of the data listed in the **Environment** tab, in the top-right panel of RStudio.

You will see a nicely formatted table, as you would in a programme like Excel.

Data tables in R (i.e. spreadsheet-like data) are called **data frames** or **tibbles**.[^1]

[^1]: A **tibble** is a special data frame. We will learn more about tibbles in the following weeks.

The `shallow` data frame contains 11 columns (called variables in the `Environment` tab). The 11 columns are the following:

-   `Group`: `L1` vs `L2` speakers of English.
-   `ID`: Subject unique ID.
-   `List`: Word list (A to F).
-   `Target`: Target word in the lexical decision trial.
-   `ACC`: Lexical decision response accuracy (`0` incorrect response, `1` correct response).
-   `RT`: Reaction times of response in milliseconds.
-   `logRT`: Logged reaction times.
-   `Critical_Filler`: Whether the trial was a `filler` or `critical`.
-   `Word_Nonword`: Whether the Target was a real `Word` or a `Nonword`.
-   `Relation_type`: The type of relation between prime and target word (`Unrelated`, `NonCostituent`, `Constituent`, `Phonological`).
-   `Branching`: Constituent syntactic branching, `Left` and `Right` (shout out to Charlie Puth).

::: callout-note
#### Quiz 3

```{r}
#| label: quiz-3
#| results: asis
#| echo: false

opts_3 <- c(
   "11",
   "650",
   answer = "6500"
)

cat("How many rows does `shallow` have?", longmcq(opts_3))
```
:::

Great stuff! See how easy it was to import data?

::: callout-tip
## Data formats

Most of the data we will be using in this course will be in the `.csv` format.

**The comma separated values format (`.csv`) is the best format to save data in** because it is basically a plain text file, it's quick to parse, and can be opened and edited with any software (plus, it's not a proprietary format).
:::

However, you can easily import other file types as long as they are tabular or rectangular (i.e. spreadsheet-like), like Excel files.

::: {.callout-note collapse="true"}
#### How to import other file types

Sometimes though you might have to read other file types, for example Excel spreadsheets.

It's easy to read other file type using the other `read_*()` functions:

-   For Excel files (i.e. files with a `.xls` or `.xlsx` extension), you need to attach the [readxl](https://readxl.tidyverse.org) package with `library(readxl)` and use the `read_excel()` function.

-   Use `read_tsv()` from the readr package (installed with the tidyverse) to read tab separated values files (these have either the `.txt` or `.tsv` extensions.

-   The flexible `read_delim()` function allows you to read a wide variety of delimited files.
:::

::: {.callout-note collapse="true"}
#### Practice 1

Download the following files into the data folder. Read them in R, making sure you use the right `read_*()` function.

-   [takete_maluma.txt](../data/takete_maluma.txt) (a tab separated file)

-   [Relatives.xlsx](../data/Relatives.xlsx)

-   Go to <https://datashare.ed.ac.uk/handle/10283/4006> and download the file `conflict_data_.xlsx`. Read both sheets ("conflict_data2" and "demographics"). Any issues?
:::

## R scripts

So far, you've been asked to write code in the **Console** and run it there.

But this is not very efficient. Every time, you need to write the code and execute it in the right order and it quickly becomes very difficult to keep track of everything when things start getting more involved.

A solution is to use **R scripts**.

::: callout-tip
#### R script

An **R script** is a file with the `.R` extension that contains R code.
:::

For the rest of this tutorial, you will write all code in an R script.

### Create an R script

First, create a folder called `code` in your RStudio project folder. This will be the folder where you will save all of your R scripts.

Now, to create a new R script, look at the top-left corner of RStudio: the first button to the left looks like a white sheet with a green plus sign. This is the `New file` button. Click on that and you will see a few options to create a new file.

Click on `R Script`. A new empty R script will be created and will open in the File Editor window of RStudio.

![](/img/new-script-button.png)

**Note** that creating an R script does not automatically saves it on your computer. To do so, either use the keyboard short-cut `CMD+S`/`CTRL+S` or click on the floppy disk icon in the menu below the file tab.

![](/img/%20save-button.png)

Save the file inside the `code/` folder with the following name: `tutorial-w02.R`.

::: callout-warning
Remember that all the files of your RStudio project don't live inside RStudio but on your computer.

So you can always access them from the Finder or File Explorer! **However**, do not open a file by double clicking on it from the Finder/File Explorer.

Rather, **open the RStudio project by double clicking on the `.Rproj` file** and then open files from RStudio to ensure you are working within the RStudio project and the working directory is set correctly. (See the tutorial from Week 1).
:::

### Write code

Now, let's start filling up that script!

Generally, you start the script with calls to `library()` to load all the packages you need for the script.

Now we only need one package, tidyverse, but in most cases you will need more than one! The best practice is to attach all of packages first, in the top of your script. Please, get in the habit of doing this from now, so that you can keep your scripts tidy and pretty!

Go ahead, write the following code in the top of the `tutorial-w02.R` script.

```{r}
#| label: read-shallow
#| message: false
library(tidyverse)

shallow <- read_csv("./data/shallow.csv")
```

Wait, what is that `"./data/shallow.csv"`? That's a **relative path**. We briefly mentioned relative paths above, but let's understand the details now.

::: callout-warning
Please, don't include `install.packages()` in your R scripts!

Remember, you only have to install a package once, and you can just type it in the Console.

But **DO** include `library()` in your scripts.
:::

### Relative paths

::: callout-tip
#### Relative path

A **relative path** is a file path that is relative to a folder, which is represented by `./`
:::

When you are using R scripts in RStudio projects, the `./` folder paths are relative to is the project folder! This is true whichever the name of the folder/project and whichever it's location on your computer.

For example, if your project it's called `awesome_proj` and it's in `Downloads/stuff/`, then if you write `./data/results.csv` you really mean `Downloads/stuff/awesome_proj/data/results.csv`!

**How does R know the path is relative to the project folder?**

That is because when working with RStudio projects, all relative paths are relative to the project folder (i.e. the folder with the `.Rproj` file)!

The folder which relative paths are relative to is called the **working directory** (directory is just another way of saying folder).

::: callout-tip
#### Working directory

The **working directory** is the folder which relative paths are relative to.

When using RStudio projects, the working directory is the project folder.
:::

The code `read_csv("./data/shallow.csv")` above will work because you are using an RStudio project and inside the project folder there is a folder called `data/` and in it there's the `shallow.csv` file.

So from now on I encourage you to use RStudio projects, R scripts and relative paths always!

The benefit of doing so is that, if you move your project or rename it, or if you share the project with somebody, all the paths will just work because they are relative!

::: {.callout-note collapse="true"}
#### Get the working directory

You can get the current working directory with the `getwd()` command.

Run it now in the Console! Is the returned path the project folder path?

If not, it might be that you are not working from the RStudio project. Check the top-right corner of RStudio: is the project name in there or do you see `Project (none)?`

If it's the latter, you are not in an RStudio project, but you are running R from somewhere else (meaning, the working directory is somewhere else).
:::

### Run the script!

Finally, the time has come to **run the script**.

There are several ways of doing this. The most straightforward is to click on the `Run` button. You can find this in the top-right corner of the script window.

![](/img/run-button.png)

An alternative way is to place the text cursor on the line of code you want to run and then press `CMD+ENTER`/`CTRL+ENTER`. This will run the line of code and move the text cursor to the next line of code.

You can even select multiple lines of code (as you would select text) and press `CMD+ENTER`/`CTRL+ENTER` to run multiple lines of code!

Now that you know how to use R scripts and run code in them, I will assume that you will keep writing new code from this tutorial in your script and run it from there!

Just another tip: sometimes we might want to add a few lines of text in our script, for example to take notes.

You can add so-called **comments** in R scripts, simply by starting a line with `#`.

For example:

```{r}
#| label: comments

# This is a comment. Let's add 6 + 3.
6 + 3
```

::: callout-note
#### Quiz 4

```{r}
#| label: quiz-4
#| results: asis
#| echo: false

cat("Is the following a valid line of R code?", torf(TRUE))
```

``` r
sum(x + 2) # x = 4
```
:::

## Data transformation

**Data transformation** is a fundamental aspect of data analysis.

After the data you need to use is imported into R, you will have to filter rows, create new columns, or join data frames, among many other transformation operations.

In this tutorial we will learn how to obtain summary measures and how to count occurrences using the `summarise()` and `count()` functions.

### Summary measures

During the lecture, we have learnt two types of measures.

::: callout-tip
#### Summary measures

**Measures of central tendency** (mean, median, mode) indicate the typical or central value of a sample.

**Measures of dispersion** (min-max, range, standard deviation) indicate the dispersion of the sample values around the central tendency value.
:::

When you work with data, you always want to get summary measures for most of the variables in the data.

Data reports usually include summary measures. It is also important to understand which summary measure is appropriate for which type of variable.

We have covered this in the lecture, so we won't go over it again here. Instead, you will learn how to obtain summary measures using the `summarise()` function from the [dplyr](https://dplyr.tidyverse.org) tidyverse package.

`summarise()` takes at least two arguments:

-   The data frame to summarise.

-   One or more summary functions.

For example, let's get the mean the reaction time column `RT`. Easy!

```{r}
#| label: mean

summarise(shallow, RT_mean = mean(RT))

```

Great! The mean reaction times of the entire sample is 867 ms.

What if we want also the standard deviation? Easy!

```{r}
#| label: sd

summarise(shallow, RT_mean = mean(RT), RT_sd = sd(RT))

```

```{r}
#| label: comments-2

# This is a comment. Let's add 6 + 3.
6 + 3
```

Now we know that reaction times are on average 867 ms long and have a standard deviation of about 293 ms (rounded to the nearest integer).

Let's go all the way and also get the minimum and maximum RT values.

```{r}
#| label: minmax

summarise(
  shallow,
  RT_mean = mean(RT), RT_sd = sd(RT),
  RT_min = min(RT), RT_max = max(RT)
)

```

Fab! When writing a data report, you could write something like this.

> Reaction times are on average 867 ms long (SD = 293 ms), with values ranging from 0 to 1994 ms.

We will learn more about standard deviations from Week 4 on, but for now just think of this as a relative measure of how dispersed the data are around the mean: the higher the SD, the greater the dispersion around the mean, i.e. the greater the variability in the data.

When required, you can use the `median()` function to calculate the median, instead of the `mean()`. Go ahead and calculate the median reaction times in the data. Is it similar to the mean?

::: callout-note
#### Quiz 5

```{r}
#| label: quiz-5
#| results: asis
#| echo: false

opts_5a <- c(
   "It changes `NA`s to `FALSE`.",
   "It converts `NA`s to `0`s.",
   answer = "It removes `NA`s before taking the mean."
)

cat("a. What does the `na.rm` argument of `mean()` do?", longmcq(opts_5a))

opts_5b <- c(
   answer = "`NA`.",
   "`0`.",
   "`10.66`."
)

cat("b. Which is the mean of `c(4, 23, NA, 5)` when `na.rm` has the default value?", longmcq(opts_5b))
```

::: {.callout-tip collapse="true"}
#### Hint

Check the documentation of `?mean`.
:::
:::

Note that R has a `mode()` function, but alas this is not the statistical mode. To get the mode of a categorical variable you can just count the occurrences of the values of that variable and the value that occurs the most is the mode!

Keep reading to learn how to count occurrences.

### Count occurrences

We can use the `count()` function from the [dplyr](https://dplyr.tidyverse.org) tidyverse package to count the number of occurrences for each value of a specific column. Let's count how many trials are correct, i.e. let's count occurrences in the `ACC` column.

Accuracy has been coded with `0` for incorrect and `1` for correct. We will see how this is not an ideal, although very common way, of coding binary variables. For now let's keep it as is.

The function `count()` takes the name of tibble and the name of column you want to count values in.

```{r count-corr, eval=FALSE}
#| label: count-corr

count(shallow, ACC)

```

How many *correct* responses are there in the `shallow` tibble?

Note that you can add **multiple column names, separated by commas**, to get counts for the combinations of values of each column.

Try to get counts of the combination of `ACC` and `Group` (`L1` vs `L2` participants). Replace `...` with the right code.

```{r count-corr-group, eval=FALSE}
count(shallow, ...)
```

::: {.callout-tip collapse="true"}
#### Hint

In `count()`, include the names of the two columns you want to get counts of, separated by commas.
:::

Are there differences in accuracy between the L1 and L2 group?

### Grouping data

Sometimes you might want to get summary measures for one variable depending on different values of another variable.

You can use the `group_by()` function from the [dplyr](https://dplyr.tidyverse.org) tidyverse package, together with `summarise()` to achieve that. Let's see how it works.

```{r}
#| label: group

group_by(shallow, Group) %>%
  summarise(
    RT_mean = mean(RT),
    RT_sd = sd(RT)
  )

```

The `group_by()` function takes at least two arguments:

-   The name of the tibble to group.

-   The name of the columns to group the tibble by, separated by commas.

Here we are grouping `shallow` by `Group`.

If you are baffled by that `%>%`, keep on reading.

### What the pipe?!

Wait, what is that thing, `%>%`?

It's called a **pipe**. Think of a pipe as a teleporter.

In the code above we are chaining two functions together using the pipe opeartor `%>%`. The output of the first function (`group_by()`) is "teleported" into the second function `summarise()`.

The pipe `%>%` teleports the output of the preceding function as the first argument of the following function. The output of `group_by` is a (grouped) tibble, and `summarise()` needs a tibble as its first argument.

That's why the code above works!

In fact, you can even use a pipe for the tibble of `group_by()`, like so:

```{r}
#| label: pipe

shallow %>%
  group_by(Group) %>%
    summarise(
      RT_mean = mean(RT),
      RT_sd = sd(RT)
    )

```

You see that the output of the code is the same here as it is above.

For comparison, this is what the code would look like without the pipe.

```{r}
#| label: no-pipe

grouped_shallow <- group_by(shallow, Group)

summarise(
  grouped_shallow,
  RT_mean = mean(RT),
  RT_sd = sd(RT)
)

```

Don't worry too much if the concept of the pipe is not clear yet. It should become clearer later.

::: callout-note
#### Quiz 6

```{r}
#| label: quiz-6
#| results: asis
#| echo: false

opts_6a <- c(
   "`count(shallow, Target)`.",
   answer = "`shallow %>% distinct(Target) %>% count()`.",
   "`shallow %>% count(Target)`."
)

cat("a. Which of the following returns the number of words in `shallow`?", longmcq(opts_6a))

cat("b. The output of `summarise()` always drops the grouping.", torf(FALSE))
```

::: {.callout-tip collapse="true"}
#### Hint

For 6b., check the documentation of `?summarise`.
:::
:::

::: {.callout-note collapse="true"}
#### Practice 2

-   Go to <https://github.com/stefanocoretta/infant-gestures> and download the `gestures.csv` file in the `data/` folder. This is data from the paper \[A Cross-Cultural Analysis of Early Prelinguistic Gesture Development and Its Relationship to Language Development\](https://doi.org/10.1111/cdev.13406).

-   Read the file in R.

-   Calculate the following:

    -   Measure of central tendency and dispersion for the `count` column (it contains the number of gestures performed by each child in different tasks).

    -   Measure of central tendency and dispersion for the `count`column grouped by `month` (the child's age).

    -   Total number of gestures by children (`dyad`).

    -   Number of children by `background`.

-   Write a short paragraph where you report the measures. You can right this as comments in your R script. For examples, see above.

::: {.callout-tip collapse="true"}
#### Hint

-   To calculate the total number of gestures by children, you need the `sum()` function.

-   To calculate the number of children by background, you need the `distinct()` function.
:::

::: {.callout-warning collapse="true"}
#### Solution

Have you tried doing the exercise and couldn't work it out?

The you can check the code solution here...

::: {.callout-important collapse="true"}
#### Code

``` r
gestures %>%
  summarise(
    count_med = median(count, na.rm = TRUE),
    count_min = min(count, na.rm = TRUE),
    count_max = max(count, na.rm = TRUE),
    count_range = count_max - count_min
  )

gestures %>%
  group_by(months) %>%
  summarise(
    count_med = median(count, na.rm = TRUE),
    count_min = min(count, na.rm = TRUE),
    count_max = max(count, na.rm = TRUE),
    count_range = count_max - count_min
  )

gestures %>%
  group_by(dyad) %>%
  summarise(
    count_tot = sum(count)
  )

gestures %>%
  distinct(background, dyad) %>%
  count(background)
```
:::
:::
:::

## Summary

You made it! You completed this week's tutorial.

Here's a summary of the R functions you learnt.

::: {.callout-note appearance="minimal"}
`library()` attaches R packages.

`read_csv()` reads `.csv` files in R.

`summarise()` allows you to calculate measures of central tendency and dispersion (with `mean()`, `median()`, `min()` and `max()`).

`count()` lets you count the number of occurrences of levels in a categorical variable.

`group_by()` allows you to group a tibble according to one or more variables.

The pipe `%>%` can be used to chain multiple functions.
:::
