EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

*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 Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW