This page contains extra R content not covered in the demonstrations and could be considered supplementary to the module. This content is useful for completing the advanced exercises from Week 1.

Lists

Lists are the most flexible objects in R. Similar to vectors, lists are an ordered set of elements; however, unlike vectors, the elements within a list can contain different classes of objects. For example, the first element of a list could be a single numeric vector, the second could be a character vector, the third could be an entire data.frame, and fourth could be another list! Lists are most useful for advanced coding or creating your own R functions. For all content covered in this module, using vectors will be sufficient.

To create a list, use the list() function, where each argument is a separate element in the list.

list(element1,element2,etc)

Elements of a list can be accessed individually by using indexing. In a list, this involves using double square brackets [[...]] that contains the number of the element you wish to select.

So, if you wish to select the third element of a list called this_list, you would run the following code:

this_list[[3]]

You could then run any operation or function on this object as if it were any ordinary object. For instance, if the third element of the list above was a numeric vector, and you wanted to multiply this vector by 3, you could use the following code:

this_list[[3]] * 3

Indexing and Vectors

Similar to how elements of a list can be accessed individually by using indexing, indexing can also be used to select individual elements of a vector. The difference between indexing for lists and vectors, however, is that vectors only use single square brackets.

So, if you wish to select the third element of a vector called this_vector, you would run the following code.

this_vector[3]

To select a range of elements, you can use a colon. For instance, the code below selects all elements between the third and the ninth element.

this_vector[3:9]

You can even use a numeric vector within the indexing. The code below selects only the first, third, and fifth elements:

this_vector[c(1,3,5)]

Matrices

Matrices are R objects in which the elements are arranged in a two-dimensional rectangular layout, similar to a data.frame. However, unlike a data.frame, all elements within a matrix is of the same class. Matrices are helpful when you need to store multi-dimensional data, but we tend not to use them in psychological research. In almost all cases, a data.frame will suit our needs better.

You can create a matrix from scratch using the matrix() function. The first argument is a vector input which will become the elements of the matrix. You will also need to specify the number of rows, and columns, and whether you want the input to be read in by row or by column.

The following code makes a 2 x 3 matrix that is read in by row.

the_matrix <- matrix(c(1,2,3,4,5,6),nrow = 2,ncol = 3,byrow = TRUE)

You can also convert data.frames to matrices and vice versa using the following functions.

the_matrix <- matrix(c(1,2,3,4,5,6),nrow = 2,ncol =2,byrow = TRUE)

#Convert to data.frame
matrix_reloaded <- as.data.frame(the_matrix)

#Convert back to matrix
matrix_revolutions <- as.matrix(matrix_reloaded)

Indexing Matrices and Data.frames

Similar to lists and vectors, individual elements of matrices and data.frames can be accessed via indexing. Indexing is the same for both matrices and data.frames. To select an individual element, use the square brackets after the name of the matrix/data.frame followed by the row number, then a comma, then the column number.

So to select the element in the 2nd row and the 3rd column from the following data.frame, you would use the following code:

this.data[2,3]

Rather than indexing a single element from a data.frame, you can also index an entire column. This is a handy shortcut to select all responses on a variable in a data.frame. This is done by calling the data.frame and variable you want to select, and separating them using the ‘$’ symbol.

For example, if you wanted to select the variable var2 in the data.frame called this.data, you could use the following code:

this.data$var2

Advanced Exercises

If you would like to practice the skills on this page, weekly exercise questions on this content are available in the advanced exercises for Week 1. You can download the interactive exercises by clicking the link below.

Click here to download this week’s exercises.