R Lists

If you want to store data of different types, then use Lists in the R programming language. A list is ordered and can be changed. In this lesson, we will learn how to create a List and work on some List operations:

  • Create a List
  • Length of List
  • Access List Items
  • Find if a specific item is present in a List
  • Change the value of a specific List item

Create a List

To create a List in the R programming language, use the list() function. Let us see an example to learn how to create a List:

Output

Let us see another example to create a List with different datatypes:

Output

Length of list

The length() method is used in the R programming language to get the length of a list. Let us see an example:

Output

Access List Items

use the [] brackets in R to access the list items i.e. elements of a List. Just set the position number of the element you want to fetch. Let us see an example:

Output

In the above example, we have accessed the 3rd item since we have set position 3.

Find if a specific item is present in a List

Use the %in% operator in the R programming language to find if an item i.e. element is in a List. If the item is present, TRUE is returned, else FALSE.

Let us see an example to find items in a List:

Output

Change the value of a specific List item

A List can be changed after creating it. You can easily change any list item using the position of that specific item. In the below example, we will change the item at position 3. Let us see the example:

Output

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

R Vectors
R Matrices
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading