[1] 3
[1] 2
Objects are created with the assignment arrow <-; = is only used to set the arguments of a function
[1] 3
[1] 2
Object names must start with a letter!! NOT “name” <- … but name <- …
In R we can combine several relations to evaluate a given statement.
| Function | Name | Example | Result |
|---|---|---|---|
& |
Conjunction (AND) | x>25 & x<60 |
TRUE |
| |
Inclusive disjunction (OR) | x>25 | x>60 |
TRUE |
! |
Negation (NOT) | !(x<18) |
TRUE |
a value: the value of the element, which can be of any type, for example a number or a string of characters
a position index: a positive integer that identifies its position within the vector.

Vectors can be created with the c() command, writing inside the brackets the values of the elements in the desired order, separated by commas.
[1] 1 2 3 4
[1] 1 2 3 4
[1] "R" "R" "R" "ok"
[1] "R" "R" "R" "ok"
A vector must be made of elements all of the same type!
[1] "character"
[1] "1" "2" "3" "don't know" "4"
Otherwise you “risk” everything being converted to character.
We can test or convert (when possible) the type of a vector with the is. & as. functions
We can select, remove and extract elements simply by using the position index inside square brackets vector[pos]
[1] 8 57 8 54 50 42 85 48 22 34 45 48 34 43 39 15 30 61 85 6
[1] 8
[1] 8 57 8 54 50
[1] 8 54 57 22
[1] 6
In the same way, we can extract all the elements of the vector except some
Indexing by position is the simplest and most intuitive approach. We can also select using TRUE and FALSE values: we can extract elements from the vector based on specific logical conditions
Factors can be created either by converting a character vector with the as.factor() command or by explicitly creating a factor with the factor() command
In practice, they assign a label to an integer value:
Factors store levels() as metadata, regardless of which ones are actually present in the vector
[1] "ciao" "hello" "hola"
[1] hello hello hola hola
Levels: ciao hello hola
However, we can drop the levels that are no longer needed with the droplevels() command
We can also rename the levels of a factor
[1] hello hello ciao ciao hola hola
Levels: ciao hello hola
[1] TRUE TRUE TRUE TRUE TRUE TRUE
[1] english english italian italian spanish spanish
Levels: italian english spanish
[1] hello hello ciao ciao hola hola
Levels: ciao hello hola
We can both reorder and rename the levels of a factor with the factor() function
[1] hello hello ciao ciao hola hola
Levels: ciao hello hola
[1] 2 2 1 1 3 3
[1] hello hello ciao ciao hola hola
Levels: hello ciao hola
[1] 1 1 2 2 3 3
[1] english english italian italian spanish spanish
Levels: english italian spanish
Remember: the levels argument inside the factor() function is used to reorder the levels of the factor, while the levels() function renames them, it does not reorder them!
[1] hello hello ciao ciao hola hola
Levels: ciao hello hola
[1] 2 2 1 1 3 3
[1] ciao ciao hello hello hola hola
Levels: hello ciao hola
[1] 2 2 1 1 3 3
Lists are flexible structures that can contain objects of different types and different sizes (each element can itself be a list)
Although a list is one-dimensional, it develops in depth
First Level: list
Second Level: numeric vector
Third Level: integer
We can assign names to the elements of a list…
… and access them by calling them by “name” with the $ operator
Like vectors, lists also have a length (length()) and possibly names (names()). The str() (structure) command is very useful for lists because it gives an overview of their structure:
The difference between the square brackets is whether we want to subset the list, getting another list, or directly access the inner element:
If we want to select several elements (i.e. actually subset the list) we must always use single square brackets:
Lists have a one-dimensional structure that can develop in depth. To select nested elements we can chain several brackets:
Matrices are a two-dimensional data structure (characterized by 2 dimensions, dim()), where the number of rows is dimension 1 and the number of columns is dimension 2.
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
They can contain only one type of data
Since they are two-dimensional, we need two position indices (rows and columns) to identify an element
They can be seen as a set of single vectors
The number of rows and columns does not have to be the same (square matrix), but the size of the matrix must be compatible with the data vector:
What does R do by default?
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 2 1 2
[3,] 1 2 1
warnings: the function informs us about something potentially problematic, but (more or less!!) everything is fine
To identify one or more elements of a matrix we need row and/or column indices separated by a comma, again inside square brackets: matrix[row, column]
We can also select an entire row or column
As with vectors, here too we can apply logical indexing:
Vectors are created with the c() function and can be concatenated using the same function:
Similarly, matrices can be joined together with the cbind() and rbind() commands:
The dataframe is the most “complex”, useful and powerful structure in R. Intuitively it is an excel sheet, while from R’s point of view it is a type of list with some properties/restrictions
With the data.frame() function we can create a dataframe…
The dataframe has both the attributes of a list, i.e. the names, and the attributes of a matrix, i.e. the dimensions (rows and columns)
We can use either square brackets [] or the dollar sign $
One of the most common operations you will deal with is extracting/evaluating a subset of the values in your dataset:
col1 col2 col3
1 1 a 1.0448101
2 2 b 0.1357442
3 3 c 0.1539800
4 4 d -1.1813148
my_df[my_df$col1 > 1 & my_df$col1 < 4, ]
my_df[my_df$col1== 1, "col3"]
my_df[my_df$col1== 1, 3]
There are also alternative, more compact ways of indexing. For example, using the subset() function:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
25 4.8 3.4 1.9 0.2 setosa
45 5.1 3.8 1.9 0.4 setosa
equivalent to:
We can also select columns rather than rows with the select argument:
We can also combine the two:
Sepal.Length Species
1 5.1 setosa
2 4.9 setosa
3 4.7 setosa
equivalent to:
Most of the time you will access variables with the $ operator. This command can also be used to create a new variable…
'data.frame': 150 obs. of 6 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
$ sum : num 8.6 7.9 7.9 7.7 8.6 9.3 8 8.4 7.3 8 ...