Home exercises: Shrub volume data set
Exercise 6: Data wrangling basics
Dr. Granger is interested in studying the factors controlling the size and carbon storage of shrubs. She has conducted an 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 the file shrub-volume-data.csv and save it into your “data-raw” folder.
- Create a new Rmd file called “shrub_volume.Rmd”. Write a short paragraph describing the data set before starting your analysis. Include a figure.
- Import the data set using
read.csv()
. Assign it to an object calledshrub_data
. - Select the data from the “length” column and print it out (using
select()
). - Select the data from the site and experiment columns and print it out (using
select()
). - Add a new column named “area” containing the area of the shrub, which is the length times the width (using
mutate()
). - Sort the data by length (using
arrange()
). - Filter the data to include only plants with heights greater than 5 (using
filter()
). - Filter the data to include only plants with heights greater than 4 and widths greater than 2 (using
,
or&
to include two conditions). - Filter the data to include only plants from Experiment 1 or Experiment 3 (using
|
for “or”). - Filter the data to remove rows with null values in the
height
column (using!is.na
) - Create a new data frame called
shrub_volumes
that includes all of the original data, plus a new column containing the volumes (length * width * height), and display it.
Exercise 7: Data aggregation
Dr. Granger wants some summary data of the plants at her sites and for her experiments. If the file shrub-volume-data.csv is not already in your work space download it.
The following code calculates the average height of a plant at each site:
shrub_dims <- read.csv('shrub-volume-data.csv')
by_site <- group_by(shrub_dims, site)
avg_height <- summarize(by_site, avg_height = mean(height))
- Modify the code to calculate and print the average height of a plant in each experiment.
- Use
max()
to determine the maximum height of a plant at each site. - Use the code you wrote to solve question (2) but as a pipeline (using the pipe
|>
or%>%
). Make sure you get the same result.
Exercise 8: Fix the code
The following code is supposed to import the shrub volume data and calculate the average shrub volume for each site and, separately, for each experiment.
read.csv("shrub-volume-data.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))
- Fix the errors in the code so that it does what it’s supposed to.
- Add a comment to the end of each line of code explaining what it does and how you modified it so that it works.