Simple
Free
Flexible
Trendy
Open a new R script file (File > New File > R Script)
Console
Console
Script
Console
Script
Tracking panel
Console
Script
Tracking panel
Multipurpose panel
Check files in your computer, see plots, manage packages, read help section of a function.
Console
Script
Tracking panel
Multipurpose panel
Caution
Write everything you do in scripts to avoid loosing your work.
Tip
Solving errors is an important skill to learn.
Tip
Comment your script to help you remember what you have done.
Complex type
Complex type
Note
These are the basic complex types. It exists a lot of different complex objects which mix all these basic objects.
if
, else
, TRUE
, FALSE
)valid.name
, valid_name
, valid2name3
valid name
, valid-name
, 1valid2name3
Tip
Avoid random names such as var1
, var2
. Use significant names: gene_list
, nb_elements
The name of the object followed by the assignment symbol and the value.
You can use operators on objects to modify them. Depending on the object format, operators have different behaviors and some are forbidden.
+
-
*
/
^
or **
%/%
%%
Exercise
Correction
Some operations raise errors and others quietly return unexpected results.
function_name(object, parameter1 = ..., parameter2 = ...)
help
, ?
or F1
)function_name(object, parameter1 = ..., parameter2 = ...)
help
, ?
or F1
)Note
Some functions are in the default installation of R. Other functions come from packages. You can also create your own functions.
c()
Concatenate function is the most common way to create vectors1:10
Easy way to create a vector with numbers from 1 to 10c()
Concatenate function is the most common way to create vectors1:10
Easy way to create a vector with numbers from 1 to 10Extra ways to create vectors
seq()
Create a sequence of numbersrep()
Repeat elements several timesrunif()
Simulate random numbers from Uniform distribution. Same for rnorm()
, rpois()
…Instructions
vector
with 7 numeric valuesvector
with 7 character valuesUsing index/position between []
length()
Number of elements in the vectornames()
Get or set the names of the vector’s value. No value by default.sort()
Sort a vectorsample()
Shuffle a vectorrev()
Reverse a vectorExtra
log/log2/log10
Logarithm functionssqrt
Square-root functionhead()/tail()
Print the first/last valuessummary()
Summary statisticsmin()/max()/mean()/median()/var()
Minimum, maximum, average, median, variancesum
Sum of the vector’s valuesInstructions
runif
or rnorm
)matrix()
: Creates a matrix from a vector
rbind()/cbind()
: Binds multiple vectors of a same length to create a matrix
mat[i,j]
: To select element at row i and column j. i and j can be vectors to select multiple elements.
mat[i,j]
: To select element at row i and column j. i and j can be vectors to select multiple elements.
t()
: To transpose the matrix
rbind()/cbind()
: To concatenate matrix vertically or horizontally
Instructions
dim()
: Returns the dimension of the matrix, e.g. number of rows and columns rownames()/colnames()
: Get or set the names of the rows/columns
length()
, head()
, tail()
For numeric matrix: min()
, max()
, sum()
, mean()
, summary()
…
Arithmetic operations: +
, -
, *
, /
Instructions
apply()
function1
means row and 2
means columnsInstructions
Some functions are wrappers of apply()
:
rowSums()
equivalent to apply(, 1, sum)
colSums()
equivalent to apply(, 2, sum)
colMeans()
equivalent to apply(, 1, mean)
rowMeans()
equivalent to apply(, 2, mean)
Objects
numeric
, character
and boolean
vector
s, list
s, matrice
s and data.frame
sVectors
vector
is a sequence of elements that are all of the same type[]
are used to manipulate elements of a vectorMatrix
matrix
is a table containing elements of one data typematrix()
: function to create a matrix[i,j]
: used to manipulate and replace elementsrownames()
, colnames()
: get/set the row or column namesapply()
: run a function on each row or columnA data frame is like a matrix but it can be composed of different data types.
data.frame()
To create a data frame
as.data.frame()
To transform a matrix into a data frame
[]
or $name
To select elements of a data.framet()
To transposehead()/tail()
To show parts of a data framedim()
: Returns the dimension of the data frame, e.g. number of rows and columns rownames()/colnames()
: Get or set the names of the rows/columns rbind()/cbind()
: To concatenate data frame vertically or horizontally
Instructions
You can use arithmetic operations (+-*/
) only on numeric columns. Functions like apply()
can be used as with a matrix.
Instructions
File path
To tell R where to find your data, you need to specify the path to the file. There are two types of paths for the same file:
Paths are always enclosed in double quotes ("
).
Working directory
Change the directory where R is working
``
Easy but important
read.table()
To read a data.frame from a multi-column file
file=
the path to the fileheader=
Set to TRUE if the 1st line correspond to column namesas.is=
Set to TRUE to read the values as simple type, recommendedsep=
The character that separate the columns, e.g. ,
or \t
(tabs)row.names=
The column number to use as row names ``Instructions
Import dataForBasicPlots.tsv into an object called mat.ge
write.table()
To write a data.frame in a multi-column file
df
the data.frame to writefile=
the file namecol.names=
TRUE print the column names in the first linerow.names=
TRUE print the row names in the first columnsquote=
TRUE surround character by double quotessep=
the character that separates each column. ’ ’ by default.saveRDS()
Save one R object into a file. Use .rds extensionsave()
Save multiple R objects into a file. Use .RData as extensionsave.image()
Save the entire R environmentreadRDS()
Read a R object from a .rds fileload()
Load R objects from a .RData fileEasy way
Automatic way
pdf()
, png()
, jpeg()
…)dev.off()
hist(x)
Plot a histogram, eg. the value distribution of a vector.
x
The vector with the values to plot
barplot(x)
Plot a barplot, eg. one bar for each value of a vector.
x
The vector with the values to plot
plot(x, y)
Plot one vector against the other using points for each element.
x
The first vector to plot (x-axis)
y
The second vector to plot (y-axis)
type
How the points are plotted. “p” for points, “l” for points joined by lines
boxplot(x)
Plot the distribution of variables.
x
The matrix of distributions
main=
A title for te plot
xlab=/ylab=
A title for the x/y axis
xlim=/ylim=
A vector of size two defining the desired limits on the x/y axis
Extra parameters
col=
The colour of the points/lines
pch=
The shape of the points
lty=
The shape of the lines
Extra functions
lines()
Same as plot but super-imposed to the existent one abline()
Draw a vertical/horizontal line
Instructions
Generate plots using the dataForBasicPlots.tsv file and save them:
“Table” objects
A matrix
is a table containing elements of one data type
A data.frame
is a table with columns of different data types
matrix()
, data.frame()
: functions to create a matrix or a data frame
[i,j]
: used to manipulate and replace elements
rownames()
, colnames()
: get/set the row or column names
Numeric columns support arithmetic operations
apply()
: run a function on each row or column
Read and write files in R
Set your working directory, eg. the folder where you are working (Session > Set Working Directory)
You can get the path of your file if you copy/paste it on R
read.table()
: Basic function to read a table
write.table()
: Basic function to write a table
pdf()/png()/jpeg() ... dev.off()
: Save a plot in PDF/PNG/JPEG format automatically
You can save R objects/environments (saveRDS()
, save()
, save.image()
)
You can load R objects/environments (readRDS()
, load()
)
Plot
There are different functions to plot data
barplot()
hist()
boxplot()
plot()
: Scatter plot with points and/or lines
Some parameters can be used to custom your plot : main=
, xlab=
, xlim=
…
Logical type
TRUE / FALSE values
==
Are both values equal?
> or >=
Is left value greater (or equal) than right value?
< or <=
Is left value smaller (or equal) than right value?
!
NOT operator, negates the value
|
OR operator, returns TRUE if either are TRUE
&
AND operator, returne TRUE if both are TRUE
Any logical test can be vectorized.
which
returns the index of the vectors with TRUE values
Instructions
Instructions
Write a if
block that automatically classify the expression of the first gene of mat.ge into:
mean(luckyNumbers)
function()
to define a functionreturn()
specifies what will be returned by the functionThis function takes a vector as input and:
Instructions
Create a function that classify the average value of a vector. It returns:
Test your function on vectors with random numbers from 0 to 10.
Instructions
Instructions
Write a function that computes the mean values of the columns:
paste()
Pastes several characters into one
grep()
Searches for a pattern in a vector and returns the index when matched
grepl()
Searches a pattern in a vector and returns TRUE if found
strsplit()
Splits a string
Instructions
Write R commands to address each question. Only one_line command allowed. The shorter the better.