Problem Set 1
Please render this file to .html and submit via Brightspace.
R Basics
Data types
Create a vector called site_num with the values 1 through 10.
Create a matrix called pop_abund with the values 101 through 120, with 2 columns and 10 rows.
Create a copy of the dataframe iris (already available as iris) and name it iris_df. View the column names and create a new column called Petal.Area, calculated as Petal.Width times Petal.Length.
Data munging
The SmoltWeights sheet from H2SMS_practicalData.xlsx contains the weight in grams (column weight_g) from a sample of 34 wild salmon smolts (column fish_id) during their out-migration from a river to the ocean. Load the dataset and perform basic summaries (range, mean, median, number of missing values for each column). Identify (using code) which fish are missing values for weight_g.
Create a new column called weight_lb that gives the weights in pounds, where 1 kg = 2.205 lb.
What proportion of fish weigh less than 0.2 lb? How many fish is this?
Create a new data.frame called large_df that contains only the fish as large or larger than the median weight and excludes rows with missing weights.
Create a new data.frame called extreme_df that contains only fish more than 1.5 standard deviations greater than the mean and excludes rows with missing weights.
Plotting
Histograms
Create a histogram of the smolt weights, labelling the axes and omitting a plot title. Add a figure legend using #| fig-cap: "..." in the quarto document code block. Add #| label: fig-smoltHist, and then in text below, reference the figure by typing @fig-smoltHist.
When rendered, @fig-smoltHist in the text is automatically replaced by “Figure 1”, with figure numbers updated automatically. Adding a legend with #| fig-cap: "..." in your code block will automatically give that figure a number, making your life easier.
The label must start with fig- for this to work.
Note that you cannot re-use the same label.
Line plots
The dataset beaver1 is available in R, giving the body temperature through time of a female beaver (Castor canadensis), with observations every 10 minutes.
str(beaver1)'data.frame': 114 obs. of 4 variables:
$ day : num 346 346 346 346 346 346 346 346 346 346 ...
$ time : num 840 850 900 910 920 930 940 950 1000 1010 ...
$ temp : num 36.3 36.3 36.4 36.4 36.5 ...
$ activ: num 0 0 0 0 0 0 0 0 0 0 ...
Debug the following code so that it produces a line plot of body temperature by observation id. Note that time is given in the form 0330 for 3:30am (?beaver1). The obs_id column thus gives the ordered observations from first to last.
As an added challenge, add vertical dashed lines indicating when the beaver was active.
Set #| eval: true to run the code when you click Render. If you’ve successfully debugged, you should then see your plot.
Boxplots
The dataset ToothGrowth is available in R, giving the length (len) of tooth-growth cells (odontoblasts) in 60 guinea pigs under different vitamin C regimes (supp: OJ=orange juice, VC=ascorbic acid; dose: 0.5, 1, 2 mg/day). Create a boxplot of the length by dose for only the guinea pigs receiving orange juice. Write a figure caption and add a label as above.
Use ggplot() to make the same boxplot, but with supp as the fill colour.
Remember to check the column types first!
Scatter plots
Using the same ToothGrowth dataset, produce a scatter plot of odontoblast length as a function of dose, where supp is the point colour.