Learning Objectives

Following this assignment students should be able to:

  • install and load an R package
  • understand the data manipulation functions of dplyr
  • execute a simple import and analyze data scenario

Reading

Lecture Notes


Exercises

  1. -- dplyr --

    Install and familiarize yourself with the dplyr package. The library() step(s) should always be located at the very top of a script.

    install.packages("dplyr")
    
    library(dplyr)
    
    help(package = dplyr)
    

    This vignette is a great reference for data manipulation verbs to keep in mind.

  2. -- Shrub Volume Data Basics --

    This is a follow-up to Shrub Volume Data Frame.

    Dr. Granger is interested in studying the factors controlling the size and carbon storage of shrubs. This research is part of a larger area of research trying to understand carbon storage by plants. She has conducted a small preliminary experiment looking at the effect of three different treatments on shrub volume at four different locations. She has placed the data file on the web for you to download:

    Download this into your data folder and get familiar with the data by importing the shrub dimensions data using read.csv() and then:

    1. Check the column names in the data using the function names().
    2. Use str() to show the structure of the data frame and its individual columns.
    3. Print out the first few rows of the data using the function head().

      Use dplyr to complete the remaining tasks.

    4. Select the data from the length column and print it out.
    5. Select the data from the site and experiment columns and print it out.
    6. Filter the data for all of the plants with heights greater than 5 and print out the result.
    7. Create a new data frame called shrub_data_w_vols that includes all of the original data and a new column containing the volumes, and display it.
    [click here for output]
  3. -- Shrub Volume Aggregation --

    This is a follow-up to Shrub Volume Data Basics.

    Dr. Granger wants some summary data of the plants at her sites and for her experiments. Make sure you have her shrub dimensions data.

    This code calculates the average height of a plant at each site:

    by_site <- group_by(shrub_dims, site)
    avg_height <- summarize(by_site, avg_height = mean(height))
    
    1. Modify the code to calculate and print the average height of a plant in each experiment.
    2. Use max() to determine the maximum height of a plant at each site.
    [click here for output]
  4. -- Shrub Volume Join --

    This is a follow-up to Shrub Volume Aggregation.

    Dr. Granger has kept a separate table that describes the manipulation for each experiment. Add the experiments data to your data folder.

    Import the experiments data and then use inner_join to combine it with the shrub dimensions data to add a manipulation column to the shrub data.

    [click here for output]
  5. -- Fix the Code --

    This is a follow-up to Shrub Volume Aggregation. If you haven’t already downloaded the shrub volume data do so now and store it in your data directory.

    The following code is supposed to import the shrub volume data and calculate the average shrub volume for each experiment and, separately, for each site

    read.csv("data/shrub-volume-experiment.csv")
    shrub_data %>%
      mutate(volume = length * width * height) %>%
      group_by(site) %>%
      summarize(mean_volume = max(volume))
    shrub_data %>%
      mutate(volume = length * width * height)
      group_by(experiment) %>%
      summarize(mean_volume = mean(volume))
    
    1. Fix the errors in the code so that it does what it’s supposed to
    2. Add a comment to the top of the code explaining what it does
    3. In a text file, discuss how you know that your fixed version of the code is right and how you would try to make sure it was right if the data file was thousands of lines long
    [click here for output]
  6. -- Link to Databases --

    Let’s access an SQL database directly from R using dplyr.

    Either use an existing copy of the portal_mammals.sqlite database or download a new copy. You should be able to link to the surveys table in the database using:

    portaldb <- src_sqlite("portal_mammals.sqlite")
    surveys <- tbl(portaldb, "surveys")
    

    surveys will be a tbl_sqlite, which means that the table remains external to the R environment. Also, we won’t need to worry about it printing out huge numbers of rows when we look at it.

    1. Use the nrow() function to determine how many records are represented in surveys.
    2. Select the year, month, day, and species_id columns in that order.
    3. Create a new data frame with the year, species_id, and weight in kilograms of each individual, with no null weights.
    4. Use the distinct() function to print the species_id for each species in the dataset that has been weighed.
    [click here for output]
  7. -- Collect from Databases --

    This is a follow-up to Link to Databases.

    Make sure you have an existing copy of the portal_mammals.sqlite database or download a new copy.

    Notice how the solution to Link to Databases task 4 is a <derived table> and that the number of rows is unknown (??).

    1. Take this <derived table> and make it a local data frame using collect() and determine how many rows it has.
    2. Calculate the average size of a Neotoma Albigula (NL) in surveys and store the result in a local data frame.
    3. Create a local data frame with the number of individuals counted in each year of the study. If you don’t know how to count things using dplyr, take another look at the dplyr vignette. Vignettes are a great way to reference packages in R if you don’t know the name of a function you need but you know the package has that capability. In this case, you will want to search for ‘count’.

    Want a challenge?: Create a local data frame containing the average size of each rodent species for individuals captured on the Control plots. You can do this by either creating a connection to the species and plots tables and using inner_join in dplyr or by writing the query in SQL and using tbl(portaldb, sql(query)), where query is a string containing the SQL you want to run. Better yet try doing it both ways.

    [click here for output]