Lists in R Language: Create, Name, and Append

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

Each of the data types (vectors, matrices, and data frames) has some level of constraints. For example, vectors are single-column data types 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 the same.

Lists in R Language

Lists in R are a fundamental data structure used to store collections of elements. Lists offer a flexible way to organize data of different types, unlike vectors that can hold elements of the same data type. Lists in R language have no such constraints as vectors, matrices, and data frames. The element of a list can contain any type of data can contain any type of data having varying lengths for each element.

Lists are the most flexible data type in R, as the list can hold all kinds of different operations when programming. Let us consider some examples of 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 the list() keyword. For examples,

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

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

Retrieving List Elements

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

A certain element of a list can be accessed using a subsetting technique (based on 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 the required index number. For example, to access the fourth element of the list, one can use,

mylist[[4]]

Note when one uses 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]]

Naming Elements of a List

The elements of a list can be named, and elements of lists can be retrieved using the $ 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]]
Lists in R Language: Create, Name, and Append

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]
  • Remember to name elements for readability when dealing with complex data structures.
  • Lists are versatile for storing various data types, making them a powerful tool for data organization in R.

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

MCQs General Knowledge

List in R Language: A Comprehensive Guide

In R language, a list is an object that consists of an ordered collection of objects known as its components. A list in R Language is structured data that can have any number of modes (types) or other structured data. That is, one can put any kind of object (like vector, data frame, character object, matrix, and/ or array) into one list object. An example of a list is

x <- list(c(1,2,3,5), c("a", "b", "c", "d"), c(T, T, F, T, F), matrix(1:9, nr = 3) )

that contains 4 components, three of them are vectors (numeric, string, and logical) and one of them is a matrix.

List in R Language

Converting Objects to List in R Language

An object can also be converted to a list by using the as.list( ) function. For vector, the disadvantage is that each element of the vector becomes a component of that list. For example,

as.list (1: 10)

Extract components from a list

The operator [[ ]] (double square bracket) is used to extract the components of a list. To extract the second component of the list, one can write at R prompt,

list[[2]]

Using the [ ] operator returns a list rather than the structured data (the component of the list). The component of the list need not be of the same mode. The components are always numbered. If x1 is the name of a list with four components, then individual components may be referred to as x1[[1]], x1[[2]], x1[[3]], and x1[[4]].

If components of a list are defined then these components can be extracted by using the names of components. For example, a list with named components is

x1 <- list(a = c(1,2,3,5), b = c("a", "b", "c", "d"), c = c(T, T, F, T, F), 
d = matrix(1:9, nr = 3) )

To extract the component a, one can write

x1$a
x1["a"]
x1[["a"]]

To extract more than one component, one can write

x[c(1,2)]     #extract component one and two
x[-1]         #extract all component except 1st
x[[c(2,2)]]   #extract 2nd element of component two
x[[c(2:4)]]   #extract all elements of component 2 to 4
Practicing R for Statistical Computing: rfaqs.com

https://itfeature.com