R

Create a List in R

“A list is an abstract data type in computer science or programming that includes a finite number of ordered values. The values in the list can repeat/occur multiple times. We can write multiple items in a list and separate them by putting commas between them. How data is entered into a list differs by programming language. The different data types of elements can be used in the same list. A list can store one or more sub-lists, vectors, and even matrices in itself.”

How to Use the R list in Ubuntu 20.04?

Lists are heterogeneous, one-dimensional data structures. A list is a vector comprising diverse data components. The list() function is used to construct a list in R. The index value in R lists can be used to retrieve elements of a list. Unlike an array in R, a list’s indexing begins with 1 rather than 0, as in other programming languages.

Following are some examples of the list in R. In these examples, we will teach you how to create a list, how you can use them, and manipulate them in R programming language.

Example 1: Creating a Simple List in R

The list() function, along with some parameters, is used to create a list in R. The list can contain both the data of the same type or different data types. Let’s have a look at a list consisting of similar types of data.

We have created four lists of different data types. The data type is the same in each of the above lists. When we pass these arrays in the list() function, we will get the following output:

You can see that each list displays values of the same type. Now, what if we have mixed data types in a single list? We will use different data types in the list() function as parameters. When we call the list, it will display each element inside the list.

Having multiple data types in a single array was not possible. However, as shown above, we have used different data types, i.e., numeric, character, logical, and vector elements.

Example 2: Naming the Elements of the List in R

After creating a list, R allows us to access the elements in the list in a very easy way. We can name one or multiple elements inside the list, and they can be accessed with the help of their names. To name the elements, the names() function is used in which we will pass a vector containing the names of elements.

As you can see, we have specified names for the vector, matrix, and a sub-list as “player,” “rank,” and “sports,” respectively. By naming the list elements, we can now access each element from the list just by calling its specified name.

Example 3: Accessing Elements of the List in R

In R, there are two approaches to retrieving the elements of the list. The first method is to access the elements by utilizing the index number. This method is also common in accessing the elements of vectors and arrays. The second method to access the element is by the name of elements. Let’s understand this with examples below.

Accessing Elements Using Index Number
First, we have to set up a list using the list function. We will create a mixed data type list having a vector, matrix, and list.

To retrieve the element, we have to use the index number inside the square brackets “[ ]” along with the name of the variable in which the list is stored, or you can also use the list itself.

Accessing Elements Using Names
To access the elements by their names, you must have to specify the names of elements first. We could not access the list by using names if the names were not assigned.

To access the elements by using names, we have to write the element name within the square bracket along with the double quotes. You can also retrieve elements by using the dollar sign “$” before the name of the element, i.e., “(l$class)” in the example above.

Example 4: Manipulating the Element of the List in R

The R list can be updated, deleted, or added. A list element can be updated from anywhere, but if we want to add or remove an element, it will be added or removed at the end of the list. To remove an item/element from a specific index, we can assign a null value to that index. We can add an element by overriding a new value.

In the first modification, we added an element character “London” to the last of the list. As it was irrelevant to the list, we replaced it with a null value in the 2nd modification. It will delete the 4th index value, i.e., “London,” from the list elements. In the 3rd modification, we have overridden a value “D” for the 3rd element of the list. After all these modifications following will be the output.

Example 5: Convert the R List Into a Vector

In R, we cannot perform arithmetic operations with the list. To perform arithmetic operations, we can convert it into a vector by using the unlist() function. The list will be passed as an input in the unlist() function to convert the list into a vector.

In the above instance, we have created two lists, “l1” and “l2”. We have passed these lists as a parameter in the unlist() function to convert them into vectors. After converting them, we have taken a sum of these vectors to display the output.

Conclusion

In this tutorial, we have discussed the Lists in detail and how you can use them in the R programming language in Ubuntu 20.04. Now you should be familiar with the R list and should be able to use them for different applications. To make it easier for you to understand, we have implemented five examples in which we have seen how to make lists in R, how to name its elements, how to access elements of the list, how to manipulate them, and how you can convert the lists into vectors to perform arithmetic operations on them.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.