In this introduction to R, we will present the main R concepts, by introducing R variables, vectors, matrices, lists, functions and packages.
You could also have a look at the following online resources:
https://www.youtube.com/watch?v=o0Y478jOjGk
https://www.youtube.com/watch?v=u1r5XTqrCTQ
https://campus.datacamp.com/
http://tryr.codeschool.com/

Variables

In R, we have two main types of values: numeric values and characters.

1
"text"

We can save these values in variables, so we can easily access them later.

a <- 1
a

And re-use them :

a + a

Exercises:

1) Assign a number of your choice to "b"


2) Show what's in b


3) Multiply a by 2


4) Add 10 to a


5) Assign your name to an object called name


Vectors

Vectors contain several values:

b <- 1:10

You can concatenate these values with the "c" function:

c(b, b)

You can also apply mathematical formulas to them:

b + b

Exercises on vectors:

6) Concatenate a and b


7) Concatenate your name and a


8) Add a to b


Functions

Many functions exist in R, and greatly simplify your life:

(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)/10
mean(b)

The head function shows the 6 first elements of any object.
The median function computes the median of a vector.
The length function returns the length of a vector.

head(b)
head(b, n=3)
median(b)
length(b)

Matrices

Matrices can be compared to tables: they are two dimensional and can contain numbers, characters...

load("../inst/extdata/cyto.RData")
cyto

We can check the number of rows and columns in the cyto object:

nrow(cyto)
ncol(cyto)

Indexing

A specific column of a matrix can be selected by its number:

fcs_a <- cyto[, 1]
fcs_a

Or the column can also be selected by typing its column name directly:

cd45 <- cyto[, "CD45"]
cd45

Or we can select a specific row:

cell2 <- cyto[2, ]
cell2

We can also select several columns of a matrix, with the "c" function:

cyto[, c(1, 2)]

Exercises on matrices:

9) Show the 2 first rows of the cyto matrix


10) Show the FSC-A value of the first cell


Lists

Lists in R can be compared to vectors, which can contain various objects:

random_numbers <- c(1, 2, 3, 7)
random_letters <- c("A", "B", "C", "Z")
my_list <- list(my_numbers = random_numbers, 
                my_letters = random_letters)
my_list

The different items of a list can then be accessed by using the "$" and typing their name directly:

my_list$my_numbers

Exercises on lists:

11) Create a list containing your name, and job:


Packages

Packages contain sets of useful functions, which can be installed, loaded, and then used in R. A package needs to be installed only once on your computer:

# install.packages(Rtsne)

But needs to be loaded every time you wish to use it:

library(Rtsne)

The functions of an installed package can then be used. They often need the user to set parameters:

# tsne <- Rtsne(cyto)

This line would generate an error, we need to lower the perplexity as we only have 10 cells.

tsne <- Rtsne(cyto, perplexity = 2)
tsne$Y

You can always ask help to R, if you wish to know how a function works:

?Rtsne

Once we have the coordinates of the cells in the new tsne space, we can plot them with the plot function:

plot(tsne$Y)

Exports

Objetcs that were created in R can be exported in many formats to be shared.

We can for instance save our result of tsne in an excel file, using the write.xlsx function from the openxlsx package:

library(openxlsx)

write.xlsx(tsne$Y, file = "tsne_result.xlsx")

We can also, on the other hand, import excel files into R:

imported_tsne <- read.xlsx("tsne_result.xlsx")
imported_tsne

Plots can be exported from R, by opening a pdf environement. The pdf function opens a new pdf file. All the following plotting commands will automatically be plotted in the pdf file. The dev.off() function closes the pdf.

pdf("tsne_plot.pdf")
plot(tsne$Y)
dev.off()

Exercises on plots:

12) The plot function has many useful arguments (xlab to change the xaxis' name, ylab to change the yaxis' name, main to change the title...) Play with these parameters until you generate a tsne plot you like


13) You can now save your plot in a pdf file


14) Plots that are too big can also be saved in a png format, they will however result in plots of lower quality. Knowing that the png function works exactly like the pdf function, export your figure in a png file:




saeyslab/FlowSOM_workshop documentation built on Sept. 3, 2021, 9:21 a.m.