Materials

BDSI Workshop
Published

April 3, 2024

๐Ÿ•™ Schedule

Time Content
10:00โ€“10:30 Getting Started
10:30โ€“10:45 Exercise 1
10:45โ€“11:15 Working with objects
11:15โ€“11:30 Exercise 2
11:30โ€“11:40 Break
11:40โ€“12:00 Reading and writing data
12:00โ€“12:10 Exercise 3
12:10โ€“12:40 Functions, loops and conditional statements
12:40โ€“12:50 Exercise 4
12:50โ€“13:00 Wrap-up

๐Ÿ“‘ Resources

  • Learn R Chapter 1: Getting started with R & RStudio
  • Learn R Chapter 2: R programming basics
  • Learn R Chapter 3: Troubleshooting and asking for help

๐Ÿ‹๏ธโ€โ™€๏ธ Exercises

Get to know fellow workshop participants

  • Introduce yourself to those on your table.
  • Ask what their favorite number is and record this.
  • Using R, calculate the mean and standard deviation of the favorite numbers of all the participants on your table.

Using R as a calculator

Using R either on your laptop or above console, compute the following values and write the answer in the box.

  • What is \((40 + 5) \times 3\)?
  • What is \(\sqrt{2^4 - 7}\)?
  • What is \(\dfrac{e^2 + 1}{2}\) to two decimal places?
  • What is \(\log_2(9)\) to two decimal places?

Finding new functions

The following operations were not shown in the slides so you will need to find the relevant functions yourself. How would you find these? Once you find the relevant functions, compute the following values in R and enter the value in the corresponding box.

  • What is \(10! = 10\times 9 \times 8 \times \cdots \times 2 \times 1\)? This operation is referred to as โ€œfactorialโ€.
  • What is \(\cos\left(\frac{2}{3}\pi\right) + \sin(-\pi)\) to two decimal places?
  • The \(\lfloor x \rfloor\) denotes the floor function (the largest integer such that it is smaller or equal to \(x\)) and \(\lceil x \rceil\) denotes the ceiling function (the smallest integer such that it is larger or equal to \(x\)). What is \(\lfloor \frac{1}{4} \pi \rfloor + \lceil \pi e \rceil\)?

Installing R packages

  • Which function installs an R package from CRAN?
  • Which function loads an R package into your system?
  • On your own laptop, install the following packages:
    • praise,
    • readr,
    • readxl,
    • reprex,
    • sessioninfo.
  • What function is in the praise package? Try running this function on your own laptop.
Reflect on learning objectives
You should be able to:
  • Conduct elementary arithmetic operations using R
  • Navigate the RStudio interactive development environment (IDE)
  • Install external packages in R to extend functionality

Using R either on your laptop or above console, compute the following values and enter your answer in the corresponding box.

Object class

R has a number of built-in datasets (see data(package = "datasets")).

Identify the object class for the following variables:

Dataset Variable Class
PlantGrowth weight
PlantGrowth group
infert education
infert age
infert case
Harman23.cor cov
Harman23.cor center
Harman23.cor n.obs

Compute summary statistics

  • What is the median weight of plants in the control group in PlantGrowth data?
  • What is the total number of cases (case = 1) in infertility after spontaneous and induced abortion in the infert data?
  • What is the 20th value in the education variable in infert data?
  • What is the value in the fifth row, fifth column in the cov matrix in Harman23.cor data?
  • What is the mean ozone value in airquality data to two decimal places?
  • What is the number of missing values in solar radiation values in airquality data? Hint: what does is.na() do?
Reflect on learning objectives
You should be able to:
  • Comprehend various object types in R
  • Manipulate lists, matrices, and vectors in R
  • Compute basic summary statistics including mean, median, quartiles, and standard deviation using R
  • Grasp the concept of missing values within the R environment

Download the data

Filename Description Download Info
roadside-transects.csv Lowland-to-highland Roadside Transects Data, South-East Highlands, Australia, 2017.
branch-water-derived-value.xlsx The effect of salinity on branch water relations and stem hydraulic vulnerability in two co-occurring mangrove species.
GRRData.csv Human activities modulate reciprocal effects of a subterranean ecological engineer rodent, Tachyoryctes macrocephalus, on Afroalpine vegetation cover
template_Morris.xlsx Whiteheadโ€™s rat morphometrics

Download the above datasets.

  • Read the data in roadside-transects.csv. How many variables does this sheet consist of? How many observations are there?
  • Read the sheet โ€œValues for RWC figuresโ€ in branch-water-derived-value.xlsx. How many variables does this sheet consist of? How many observations are there?
  • Read the sheet โ€œKey Valuesโ€ in branch-water-derived-value.xlsx. How many variables does this sheet consist of? How many observations are there?

For more practice, see here for more datasets to try reading into R.

Reflect on learning objectives
You should be able to:
  • Import and export data in R

Using R either on your laptop or the consoles below, compute the following values and enter your answer in the corresponding box.

  • cor.test() is a function that test for association between paired samples. E.g. cor.test(mtcars$mpg, mtcars$disp) tests the association for the two variables mpg (miles per gallon) and disp (displacement) in the mtcars dataset. The \(p\)-value of the test can be accessed by cor.test(mtcars$mpg, mtcars$disp)$p.value. What is the \(p\)-value of the test of association for the speed and dist variables in the cars dataset?
  • Write a function that computes the number of distinct values in a vector. Use this to find the number of distinct values in mtcars$vs. What is the result?
  • Compute a pairwise correlation test for all pair of variables in mtcars dataset if the number of distinct values are greater than 3.

Which pair of variables has the highest \(p\)-value? (Enter it alphabetical order) var1: and var2:

Reflect on learning objectives
You should be able to:
  • Create basic functions, employ conditional statements, and utilize for loops in R
  • Decipher error messages and do basic troubleshooting

This website is brought to you by the ANU Biological Data Science Institute.