EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials R Programming Tutorial R list
 

R list

Priya Pedamkar
Article byPriya Pedamkar

Updated March 21, 2023

R list

 

 

Introduction to R List

R list is a type of data structure that supports multiple data types. R language has a built-in function called list(), which is used to create the list variable and assign various types of values to it. Data values in the list are called components, and the components can be accessed using list indexing in R languages. R list is a generic data structure that supports both the primitive data structures like integer, logical, double, character, and other data structures like vectors as its components. The list data structure is instrumental in R programming due to the adaptability of multiple data types.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

What is an R List?

A-List is a one-dimensional data structure in which each element itself is a data structure. In other words, we can say that a list is a generic vector containing other objects. A-List can contain elements of the same type or a diverse type.

1. Creating A-List

A-List is created using the list () function. The creation of a list has been explained below:

For example, we want to create a “my_list “list containing three vectors n, s, b, and a numeric value 5.

> n = c (5, 6, 7)
> s = c ("aaa", "bbb", "ccc", "ddd", "eee")
> b = c ( FALSE, TRUE, FALSE, TRUE,FALSE)

This is the syntax to store the elements in a list.

List Creation Step

> my_list = list (n, s, b, 5)

Now, we have stored all these individual data structures in a list named “my_list” using the list () function. Next, the structure of the list can be examined using the str () function.

>str(my_list)

Output:

List of 4

$: num [1:3] 5 6 7

$: chr [1:5] “aaa” “bbb” “ccc” “ddd” …

$: logi [1:5] FALSE TRUE FALSE TRUE FALSE

$: num 5

Str() function output shows each element present in the list and their data type. We can find the length of the list, i.e., how many elements are present in it, by using the length () function.

Suppose we use: length (my_list). The output will be 4 in this case as it contains 4 elements. We can check if a given data structure is a list with the help of the typeof() function.

2. Name A-List

Now we will see how we can name the components present in a list.

Code

my_list = list (a=n, b=s, c=b, d=5)
my_list$a

Output:

R list5

Now, we have given a specific name to each component of the list. If we want only one component at a time, we can fetch it in the following way:

Code 

my_list$d

Output:

R list6

We can also fetch the list elements with syntax like

my_list[b]

Here, It will give output as FALSE TRUE FALSE TRUE FALSE. This shows that we can provide unique names to the list components So that it will be easier for us to fetch the list elements individually. We can also access the list elements with the help of indexing. For example, if we want to access the first element of the list, It will be like

my_list[1] or we want the first 3 elements; it will be my_list[1:3]. Out will be as follows :

Code

For 1st : my_list[1] $a

Output:

my_list output

Code

For 2nd: my_list[1:3] $a

Output:

my_list output

Code

$b

Output:

my_list output

Code

$c

Output:

R list3

3. Manipulating A-List Element

We can add, delete or update the list elements. But please note that We can update any element, but new elements can only be added at last.

# Adding an element.
my_list[5] <- "New element"
print(my_list[5])

# Removing the element
my_list[5] <- NULL
print(my_list[5])

# Updating the 4th Element.
my_list[4] <- 6
print(my_list[4])

Conclusion

We have seen that a list is an essential component of R, making it easier to store and manipulate different types of elements. With the list’s help, it becomes possible to store different kinds of data structures in one variable and use them as required.

Recommended Articles

This has been a guide to the R list. Here we discuss an introduction to R List. You can also go through our other suggested articles to learn more –

  1. Data Analysis Tools Research | Top 14 Tools
  2. Introduction to Data Science Tools
  3. Artificial Intelligence Tools with Application
  4. Data Warehouse tools With Features
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW