Rstudio console


Ex. 1.1

Use the Rstudio console to compute:

  • The square root of 144

  • The logarithm base 2 of 24

  • The following formula

\[ \frac{2}{3}(\sqrt{9}-log_{10}{1000})^2+\frac{5}{6} \]

Have a look at the History panel: what do you see?

Tip: Once your cursor is in the Rstudio console, if you press the upwards arrow on your keyboard, you can select one of the commands you have already typed.


Variables and operations


Ex. 1.2

Assign the value 2 to a variable called x.

Assign the value 10 to a variable called y.

Check whether x and y are equal.

Check whether y and x are different.

Check whether y is greater or equal to x.

Divide y by x.

Save the remainder of the division above (i.e., the modulus) in a variable called z and print it to screen.

Is y an even number? If so, it should be divisible by 2 without remainder.

Have a look at the Environment panel: what do you see?


Ex. 1.3

Try to assign the value of 5 to a variable called 4th_var?

Why is it not possible? What can you change to make the assignment work?


Ex. 1.4

Initialize the following variables as in the code below:

Name <- "Maria"
Age <- 30
PhD <- TRUE

What are the classes of the three variables?

Check whether the PhD variable is NA.


Ex. 1.5

Initialize the following variables as in the code below:

x <- -5
y <- 0
z <- x / y

What is the value of z?


Ex. 1.6

Save the length of the base and height of the rectangle depicted below into two numeric variables called b and h:

Compute the area of the rectangle, save it into a numeric variable called A, and check whether it is bigger than 100 cm2.


Data structures: vectors and factors


Ex. 1.7

Save the first 10 integers into a vector called num and check its class. Tip: use the seq function to initialize the vector.

Save the first 5 letters of the alphabet in a vector called alpha and check its class.

Advanced: the letters object built-in in R contains all the letters of the alphabet.

Concatenate num and alpha, save the resulting vector in a variable calles mix, and check its class. Is it a numeric or character vector?


Ex. 1.8

Build a factor variable called expDesign storing the information about which mice is wild type (“WT”) or mutant (“MU”) in the figure below.

Tip: use the rep, c, and factor functions.

Check the class, length, and levels of your variable expDesign.

Assign the mice identifiers (M1, M2, …) to the same variable using the names function.

Advanced: the paste function can be used to concatenate strings.


Manipulating vectors


Ex. 1.9

Initialize the following vector in R:

age <- c(11, 12, 34, 89, 45, 90, 12)

Select only the elements of the age vector that are greater or equal to 18 by using a logical index (i.e., age >= 18) and a numerical index (i.e., which(age >= 18)).

What is the difference of the age >= 18 and which(age >= 18) indexes in terms of values, class, and length?


Ex. 1.10

Initialize the following vector in R:

x <- c(0, -1, 3, 10, -14, 7.5, 9)

Save in a vector called y only the non-negative elements of x by using:

  • Positive indexes (to indicate which elements should be selected)

  • Negative indexes (to indicate which elements should be discarded)

  • A logical vector

  • A “rule” using > or ≤


Ex. 1.11

Initialize in R the following named vector of gene expression levels:

normExpr <- c(10.2, 11.4, 4.0)
names(normExpr) <- c("CD8A", "CD8B", "PDCD1")

Access the expression of CD8A and CD8B genes by using:

  • Positive indexes

  • Vector names


Ex. 1.12

Initialize in R the following vector of estimated cell fractions:

cellFractions <- c(-0.1, 0.4, -0.4, 0.5, 0.2)

Set to 0 all negative cell fractions.