Although you will be learning R in this class, it might be more appropriate to say that you are learning the tidyverse.
The tidyverse is a set of packages that share an underlying design philosophy, grammar, and data structures. The tidyverse consists of packages that are simple and intuitive to use and will take you from importing data (with readr), restructuring and transforming data (with tidyr and dplyr), and to graphically visualizing data (with ggplot2).
The language of the dplyr package will be the underlying framework for how you will think about manipulating and transforming data in R.
dplyr uses intuitive language that you are already familiar with.
rename() renames columns
filter() filters rows based on their values in specified columns
select() selects (or removes) columns
mutate() creates new columns based on transformation from other columns, or edits values within existing columns
summarise() aggregates across rows to create a summary statistic (means, standard deviations, etc.)
These data come from a hypothetical (I made it up) research study to compare the effectiveness of two memory techniques, a mnemonic technique and a spaced repetition technique, for improving memory retention. Participants were randomly assigned to one of the two memory techniques and completed 3 memory tests (A, B, and C). The number of correctly recalled words for each memory test was recorded in the two data files by research assistants.
Example Data Set
Use what you learned from Class1 to explore the data
what are the column names?
what type of values are in each column?
It turns out that the research assistant who ran participants in the spaced repetition condition did not follow the lab’s protocol for recording data 🤦♀️
They:
used wrong column names,
recorded the memory tests as X, Y, and Z (A, B, and C, respectively),
left out what condition these data were from
gave some particpants less than 3 memory tests! 🤬
Rename columns: rename()
rename()
First, let’s fix the RA’s mistake by renaming the columns in the spaced repetition data as they are named in the mnemonic data. We can do so using the rename() function. The format for this function looks something like:
rename(new_name = old_name)
Here is how we would rename the columns in the spaced repetition data we imported.
Let’s remove rows that correspond to those participants that did not complete 3 memory tests. It turns out that those participants were always ran on Thursday or Friday, must have been a bad day for the research assistant 😢.
We can use filter() to remove rows that have Thursday or Friday in the day column.
repetition_data <- repetition_data |>filter(day !="Thursday", day !="Friday")
Select columns: select()
select()
select() allows you to select which columns to keep and/or remove.
select(columns, to, keep)
select(-columns, -to, -remove)
select() can be used with more complex operators and tidyselect functions, see the documentation here.
select()
For the repetition data, let’s only keep the following columns
mutate() is a very powerful function. It basically allows you to do any computation or transformation on the values in the data frame. See the full documentation here.
Within mutate() the = sign functions similarly to the assignment operator <-, where the result of whatever is on the right-hand side of = gets assigned to the column that is specified on the left-hand side (an existing column or a new one you are creating).
mutate()
Add a new column
We need to create a column specifying what condition the spaced repetition data came from, dang RA!
case_when() is basically a sequence of if else type of statements where each statement is evaluated, if it is true then it is given a certain value, else the next statement is evaluated, and so on.
The basic format of case_when() looks like:
mutate(a_column =case_when(a logical statement ~ a value, another statement ~ another value,.default = and another value))
case_when()
Let’s see an example of this with the spaced repetition data. We need to change the values in the word_list column so that X is A, Y is B, and Z is C.
rowwise() is used when you want to perform operations row by row, treating each row as a single group. This is useful when you want to aggregate data (e.g., mean()) across multiple columns.
The data set we are working with does not provide a good demonstration of this so let’s create a different set of data to look at how to use rowwise()
The thing is, we don’t really care about performance on each individual word_list (A, B, and C). We care about the participant’s overall performance, aggregated across all three word lists. To aggregrate data using dplyr we can use summarise().
The result of summarise() is a reduced data frame with fewer rows.
The code inside of summarise() looks a lot like the code we could put in mutate().
The difference is that mutate() does not collapse the data frame but summarise() does.
summarise()
Let’s calculate the mean recall performance by condition and participant. This will result in one row per participant (because it is a between-subject design).