Tutorial 1
Tutor notes
Installing R
First, you need to download and install the latest version of R.
- Go to R official website
- Click on “download R”
- Save the installation file
- Run the installation file and follow the prompts to install R (default settings are fine)
Installing RStudio
- Go to RStudio
- Click the big blue download button to download the installation file
- Run the installation file and follow the prompts to install RStudio (default settings are fine)
Cheatsheets
If you forget how to use it or are not sure, there are some cheatsheets for you: RStudio cheatsheet
Question set 1 – Getting started
- Q1: Create a new R script
tutorial1.R - Q2: Execute the following code
- Q3: Use RStudio tab panes to explore
fakedata - Q4: Explain the functions
mean()andsd()
Calculate mean and sd for a numeric vector. mean() is the average; sd() is the standard deviation.
- Q5: If we calculate
mean(fakedata), it will result in an error. Why does this happen, and how should we fix the error?
fakedata is a character vector because "5" forces type conversion. Coerce to numeric first:
Question set 2 – Data types
- Q1: We have 25 students in BIOF1001, to store the final marks (0 to 100 with a precision of 0.1), what data type will we use?
Numeric (double).
- Q2: For the grades (
A+toF), what data type and data structure can be used to keep the data?
character and factor.
- Q3: If you have two vectors for the BIOF1001
marksandgradesand onecharacterfor teaching performance"good", and you want to store them into one variable, which data structure will you use?
A list.
Question set 3 – Matrix manipulation
- Q1: Make a matrix named
my_matrixwith a shape of 5 rows and 2 columns, filled with values from 3 to 12, where the first row contains 3 and 4. Hint: For creating a vector from 3 to 12, you may useseq()or:.
- Q2: Based on
Q1, add the row names toDay1toDay5and column names toLunchandDinner.
- Q3: Based on
Q2, extract a matrix with a shape of 3x1 containing the values 6, 8, and 10 from the matrixmy_matrix.
Any of the following:
- Q4: What will you get for
my_matrix[c(TRUE, FALSE, FALSE, TRUE), ]? Hint: think of recycling if the index length is different from the query dimension (Over-flexibility comes with a price of wrong use).
The logical index is recycled to 5 rows, so you get rows 1, 4, and 5 (length 3).
Question set 4 – Analyzing data
- Q1: Now, in your Desktop folder, create a subfolder named
R_exercisesand download this file of differentially expressed genes results Diff_Expression_results.tsv (or link to view) to the folder. Check your current working directory bygetwd()function and change the working directory to the folder you just created. Hint: you may usesetwd()to change the working directory or use theSessionbutton of RStudio.
In RStudio (not WebR): getwd() then setwd("~/Desktop/R_exercises"), or use Session → Set Working Directory.
- Q2: Related to Q1, use the
read.table()function to load the file into a data frame with the variable namedf_DEG. Hint: You may consider using the full path or just the file name if it’s in the same working directory. Please keepheader=TRUEfor the argument. Think how to find the help page for a certain function.
- Q3: Can you calculate the
meanandstandard deviationof thelog2FoldChange?
Question set 5 – Scientific computing
- Q1: Write a function to solve a quadratic equation
\[ ax^2+bx+c=0 \]
Using only material covered so far (warns when the discriminant is negative):
With flow control (later lecture): return NaN when b^2 - 4*a*c < 0.