Use the Rstudio console to compute:
sqrt(144)
## [1] 12
log2(64)
## [1] 6
\[ \frac{2}{3}(\sqrt{9}-log_{10}{1000})^2+\frac{5}{6} \]
2/3 * ( sqrt(9) - log10(1000) )^2 + 5/6
## [1] 0.8333333
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.
Assign the value 2 to a variable called x.
x <- 2
Assign the value 10 to a variable called y.
y <- 10
Check whether x and y are equal.
x == y
## [1] FALSE
Check whether y and x are different.
x != y
## [1] TRUE
Check whether y is greater or equal to x.
y >= x
## [1] TRUE
Divide y by x.
y / x
## [1] 5
Save the remainder of the division above (i.e., the modulus) in a variable called z and print it to screen.
z <- y %% x
z
## [1] 0
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?
Try to assign the value of 5 to a variable called 4th_var?
4th_var <- 5 # Possible solution among many
## Error: <text>:2:2: unexpected symbol
## 1:
## 2: 4th_var
## ^
Why is it not possible? What can you change to make the assignment work?
var4 <- 5
Initialize the following variables as in the code below:
Name <- "Maria"
Age <- 30
PhD <- TRUE
What are the classes of the three variables?
class(Name)
## [1] "character"
class(Age)
## [1] "numeric"
class(PhD)
## [1] "logical"
Check whether the PhD variable is NA.
is.na(PhD)
## [1] FALSE
Initialize the following variables as in the code below:
x <- -5
y <- 0
z <- x / y
What is the value of z?
Save the length of the base and height of the rectangle depicted below into two numeric variables called b and h:
b <- 16
h <- 9
Compute the area of the rectangle, save it into a numeric variable called A, and check whether it is bigger than 100 cm2.
A <- b*h
A > 100
## [1] TRUE
Save the first 10 integers into a vector called num and check its class. Tip: use the seq function to initialize the vector.
num <- seq(1, 10)
class(num)
## [1] "integer"
Save the first 5 letters of the alphabet in a vector called alpha and check its class.
alpha <- c ("a", "b", "c", "d", "e")
class(alpha)
## [1] "character"
Advanced: the letters object built-in in R contains all the letters of the alphabet.
alpha <- letters[1:5]
class(alpha)
## [1] "character"
Concatenate num and alpha, save the resulting vector in a variable calles mix, and check its class. Is it a numeric or character vector?
mix <- c( num, alpha )
class(mix)
## [1] "character"
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.
expDesign <- factor( c( rep("WT", 3), rep("MT", 4) ) )
Check the class, length, and levels of your variable expDesign.
class(expDesign)
## [1] "factor"
levels(expDesign)
## [1] "MT" "WT"
length(expDesign)
## [1] 7
Assign the mice identifiers (M1, M2, …) to the same variable using the names function.
names(expDesign) <- c("M1", "M2", "M3", "M4", "M5", "M6", "M7")
expDesign
## M1 M2 M3 M4 M5 M6 M7
## WT WT WT MT MT MT MT
## Levels: MT WT
Advanced: the paste function can be used to concatenate strings.
names(expDesign) <- paste ("M", seq(1, 7), sep="")
expDesign
## M1 M2 M3 M4 M5 M6 M7
## WT WT WT MT MT MT MT
## Levels: MT WT
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)).
age[age>=18]
## [1] 34 89 45 90
age[which(age>=18)]
## [1] 34 89 45 90
What is the difference of the age >= 18 and which(age >= 18) indexes in terms of values, class, and length?
age >= 18
## [1] FALSE FALSE TRUE TRUE TRUE TRUE FALSE
which(age >= 18)
## [1] 3 4 5 6
class(age >= 18)
## [1] "logical"
class(which(age >= 18))
## [1] "integer"
length(age >= 18)
## [1] 7
length(which(age >= 18))
## [1] 4
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:
y <- x[ c(3, 4, 6, 7) ]
y
## [1] 3.0 10.0 7.5 9.0
y <- x[ -c(1, 2, 5) ]
y
## [1] 3.0 10.0 7.5 9.0
y <- x[ c(FALSE, FALSE, TRUE,TRUE, FALSE, TRUE, TRUE) ]
y
## [1] 3.0 10.0 7.5 9.0
y <- x[ x>0 ]
y
## [1] 3.0 10.0 7.5 9.0
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:
normExpr[ c(1, 2)]
## CD8A CD8B
## 10.2 11.4
normExpr[ c("CD8A", "CD8B") ]
## CD8A CD8B
## 10.2 11.4
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.
cellFractions[ cellFractions<0 ] <- 0
cellFractions
## [1] 0.0 0.4 0.0 0.5 0.2