Introduction to Tidyverse

## Welcome to the TidyVerse

The goal of this tutorial is to introduce the tidyverse a series of R packages that make data manipulation and processing easier. While most of what is covered here could be accomplished using base R functions, the tidy functions make things easier and more clear.

The material covered here is also explained in greater detail in the course textbook R for Data Science by Hadley Wickham and Garrett Grolemund. I encourage you to use that book for clarification or greater depth. It is free online.

In R there are many add on packages, a.k.a. libraries. Tidyverse is one such package. We use the library() command to load a library for use in our current R session. library() takes the package name as an argument.

### pre-requisites

To use the functions in this tutorial in your own R session you would need to use library to load the tidyverse package as shown below. I’ve pre-loaded it for this tutorial.

library(tidyverse)

## Tibbles

Optional reading: tibbles in R for Data Science

You were previously introduced to data.frame objects as spreadsheet-like objects that are useful for handling data in R. Tidyverse has a related data type, the tibble. Details of the differences aren’t important right now, but if you are already an R afficiando and are curious you can read about them in the tibbles chapter in R for Data Science

To import data into R as a tibble it is convenient to have it saved as a comma separated value (csv) file. For the rest of this tutorial we will work with a dataset of tomato measurements in the file Tomato.csv

We import the data using the read_csv() function. If you have used R before, please note that this is a different function than read.csv(). read_csv() created a tibble upon import instead of a data frame.

We will use data from a pilot experiment measuring the growth of various wild tomato accessions and species under different light conditions. The Tomato.csv file is included in your Assignment 3 repository that you cloned from github. Modify the path below so that the file is imported.

tomato <- read_csv("Tomato.csv")

Now use the head function to take a look at the imported data

• shelf, flat, col, row: information about where each plant was grown
• acs: the accession number for that strain
• trt: “H” is light with a high red:far-red ratio; simulated sun. “L” is light with a low red:far- red ratio; simulated plant shade.
• days: days since germination
• date: date of measurement
• hyp: hypocotyl length (embryonic stem)
• int1-int4: individual internode lengths. Internodes are stem segments
• leafnum: which leaf was measured
• petleng: petiole length
• leafleng: leaf length
• leafwid: leaf width
• ndvi: a measure of plant vegetation density
• lat: latitude of origin
• lon: longitude of origin
• alt: altitude of origin
• species: species of plant
• who: who measured the plants

## Filter data with filter()

The tomato data set contains data from five species. We can see their names with:

unique(tomato\$species)

Suppose that we only want to look at the data from one of these, S. pennellii. We can use the filter function to specify exactly that:

Spen.data <- filter(tomato,species == "S. pennellii")

The first argument is the data object that we want to work on, and the second argument is a logical statement describing how we want to filter.

How many rows were in the original data set and how many are in your filtered data set? Add additional lines to the code below to answer this question

Spen.data <- filter(tomato,species == "S. pennellii")

There were two researchers that measured the data. Perhaps we only trust the data gathered by one researcher. Find the names of the researchers and then filter to only keep the data measured by the second researcher in the list. How can you confirm that your filtering worked and that only one researcher is contained in the new data set?

You can provide multiple test arguments to filter() and all must evaluate to true for a row to be selected.

Try creating a new data set that consists of only S. chilense samples gathered from altitude greater than 2500 meters.

## Select columns with select()

The tomato data set has 23 data columns, and you may not care about all of them. We can use select() to select particular columns

select(tomato, species, hyp) 

If we use a minus sign - in front of a variable name then select will select everything except for that variable name:

select(tomato, -shelf, -flat, -col, -row) #remove position information

Select can also be used to rearrange the column order. Combine with everything() so that you don’t have to type them all.

So, if we want to have the “who” column first, then…
select(tomato,who,everything())

Practice by using select to create a new tibble that only has the hypcotyl and internode data, along with species, accession, and trt.

## Combine commands using pipes

You will remember the pipe | operator in Linux that takes the output from one command and “pipes” it into another command. The R Tidyverse has a similar operator, although it is written as %>%. (Technically this comes from the package magrittr (get the pun?))

So if we want to use filter to keep data collected by Dan and then select to only keep species, accession, who, trt, and leaf data we could do it like so:

Dan_leaf_data <- tomato %>% #start with the tomato data;
#pipe it to the next command (filter).
#final results will go into Dan_leaf_data
filter(who=="Dan") %>%
select(species, acs, who, trt, starts_with("leaf")) # note use of "starts_with!"

Dan_leaf_data  

Note that we no longer have to give the “tomato” argument to filter or select, pipe is taking care of that first argument for us.

Now try it yourself, selecting the hypocotyl data measured by Pepe from the “H” trt.

## Arrange data with arrange()

The arrange() function allows you to sort your data based on the column(s) of your choice:

if we want to sort by hypocotyl length:
tomato %>% arrange(hyp)

By default arrange will sort from lowest to highest. if you want to go from highest to lowest, then add desc

tomato %>% arrange(desc(hyp))

You can sort on multiple columns by providing multiple arugments to arrange. Try sorting by both accession (acs) and hyp. How does changing the order of the arguments to arrange affect the results? Try it both ways.

Now fill in the blanks the code below to create a new data set that contains data gathered by Pepe, sorted by altitude (alt) and containing the who, trt, species, acs, hyp, and alt columns.

Also you will need to fix an error in the code before this runs.

Pepe_long_hyps <- tomato %>%
______(who==Pepe) %>%
______(who, trt, species, acs, hyp, alt) %>%
______(alt)

## Create new columns with mutate()

If you want to add a new column to your data set, one way to do so is with mutate. For example, if we wanted a column that represented the leaf length to width ratio:

tomato_lw <- tomato %>%  mutate(lw=leafleng/leafwid)
tomato_lw %>% select(leafleng,leafwid,lw, everything())

Use mutate to make a new column that represents the total stem length of the plant (hypocotyl + all internodes).

## Create summaries with summarize()

Tidyverse functions also provide an easy way to summarize your data.

We first tell R what groups we are interested in summarizing (using group_by(), and then how we want it summarized (summarize()).

For example, if we wanted the mean hypocotyl length of each species, then:

tomato %>%
group_by(species) %>% #want to compute something for each species
summarize(hyp.mean=mean(hyp)) #hyp.mean is what the new summary will be called 
We can group by multiple factors:
tomato %>%
group_by(species,trt) %>%
summarize(hyp.mean=mean(hyp))

Also we can create multiple summary columns, and, of course, the results can be stored in a new object

hyp.summary <- tomato %>%
group_by(species,trt) %>%
summarize(hyp.mean=mean(hyp), hyp.sd=sd(hyp))
hyp.summary