Lists in R: Create, Name, and Append a List

Before understanding and working on lists, let’s review the other data types in R

Each of the data type (vectors, matrices, and data frames) has some level of constraints. For example, vectors are single column data type and can only store one type of data. Matrices are of two-dimensional, but they can store only one type of data. On the other hand, data frames are two-dimensional and can store different types of data, but in data frames the length of columns should be same.

Now comes to lists.

In R, lists have no such constraints as vectors, matrices and data frames have. The element of a list can contain any type of data can contain any type of data having varying length for each element.

Lists are the most flexible data type in R, as the list can hold all kind of different operations when programming. Let us consider some examples about how to create lists in R, and how elements of the list can be named, retrieved and appended. The list data type is created using list() keyword. For examples,

mylist <- list('alpha', 'beta', 'gamma', 1:5, TRUE)

Note that lists are printed differently and they have their own form of indexing for retrieving each element of the list.

Retrieving List Elements

The certain element of a list can be accessed using subsetting technique (on the basis of the list indexing mechanism. For example, to access the first three elements of the list created above, one can write in R console as,

The certain element of a list can be accessed using subsetting technique (on the basis of the list indexing mechanism. For example, to access the first three elements of the list created above, one can write in R console as,

mylist[1:3]

To examine one element of the list, double square brackets can be used with required index number. For example, to access the fourth element of the list, one can use,

mylist[[4]]

Note when one use normal subsetting, a list will be obtained as a result. When double square brackets are used the contents of the elements are obtained. Note the difference and command and output of each command used below:

mylist[4]
mylist[[4]]

The elements of a list can be named, and elements of lists can be retrieved using $operator instead of square brackets. The first command will name the elements of the mylist object. and then other commands will result in the same output. names(mylist) <- c("a", "b", "c", "d", "e") mylist$d
myslit[[4]]

Appending an Element to List

Like data frames, an element of a list can be created by assigning something to an index that does not exist yet. For example, a sixth element is added to the list

mylist$f <- c("Pass", "Fail") Now the 6th element of the list contains a vector of strings “Pass”, and “Fail”. To check this, mylist$f
mylist[[6]]

To access the elements of specific list vector subsetting and two square brackets can be used. For example to access the fourth element of the list one can use,

mylist[[4]][3] # 3rd element of 4th list element
mylist[[4]][1:3]# first three elements of 4th list element
mylist\$d[3]

For further reading about lists see the link Lists in R.