BINF_tut


Project maintained by sarbal Hosted on GitHub Pages — Theme by mattgraham

Week 8: R basics

Objectives

This tutorial will teach you background knowledge on R. You’ll learn about:

Downloads

First things first! Download these files into your working directory:

Setting up

Open up Rstudio. Start a new notebook file by selecting “File” -> “New File” -> “R Notebook” Save the file as “yourname_week5.Rmd”. Delete the instructions starting from “This is an [R…”. For the different code below, insert it as R chunks. An R chunk is code placed after a line that starts with ` { r } `and ends before a line with ` `.
For this tutorial, you will be copying the code chunks below into your R notebook as R chunks.

Defintions

print("Hello World")

Copy the above code into your console and press enter. Next, copy it as an R chunk into your notebook. Press play.

load("R_basics.Rdata")
sum(x)
plot(x,y)
require(limma)
source("helper.R")

Try it!

Simple arithmetic

Variables

x <- 1
y = 2 
very_important_variable <- 0
not_very_important = 100 

Data types and structures

Advanced data types and structures

sex[1] <- "female"
sex[2] <- "unknown" # This will cause an error!
sex # You should see the NA value 

Advanced R types

As R is an object-oriented (OO) language, there are also some OO systems (which we will not go through here). The most commonly used in bioinformatics related packages is the S4 class. For some advanced reading: http://adv-r.had.co.nz/S4.html.

Functions

Some useful functions:

length(my_list) # returns the length of an object 
sort(my_seq)   # sorts a list, vector or matrix  
ls()     # lists all the objects in your environment 

User-defined functions

my_plot(my_seq) ```

Test yourself!

  1. Install these packages (and their dependencies):
  2. In your console, generate a vector of random numbers (any which way you want) of length between 10 and 100, and assign it to a variable called “my_random_numbers”. Print out the length of this vector, and then the first and last numbers of the vector. Copy the code into your R notebook as an R chunk.
  3. Generate two square matrices (equal width and height) named B1 and B2. Multiply these matrices and save the output of the multiplication as B. Print out the first column of B1, the last row of B2, and then the diagonal of B. Copy the code into your R notebook as an R chunk.
  4. Plot any of the plots from the CatterPlot page. Copy the code into your R notebook as an R chunk.
  5. Make a matrix of dimension 20 by 40, full of zeroes. Then, modify the matrix so that once viewed, it spells out your initials OR a random shape OR pixel art. Use the image() function to view it as you go along, but remember, it plots things rotated… Once done, plot it using the image function, but remove the axes. Copy the code into your R notebook as an R chunk.
  6. “Knit” your R markdown file into a pdf or html.

Solutions

Resources and cheatsheets

https://posit.cloud/learn/cheat-sheets https://posit.cloud/learn/primers/1.2

Back to the homepage