R Lists

R Vectors

📋 R Lists

Lists in R are flexible data structures that can store different types of data together.

They are very useful when working with complex or structured data.


 1. What is a List?

  • One-dimensional data structure

  • Can contain numbers, strings, vectors, matrices, data frames, even other lists

  • Unlike vectors, elements do not need to be the same type


 2. Creating a List


 


 3. Accessing List Elements

a) By Name



 

b) By Index ([[ ]])



 

c) Using [ ] (Returns sublist)



 

📌 Difference:

  • [[ ]] → returns element

  • [ ] → returns list


 4. Access Nested List Elements


 


 5. Modifying a List

Add new element


 

Change element


 


 6. Remove List Element


 


7. List Length

length(my_list)

 8. Convert List to Vector (If Possible)

unlist(list(1, 2, 3))

 9. Apply Function on List

lapply(my_list, class)

✔ Returns data type of each element


 10. Lists vs Vectors

Feature List Vector
Data types Different Same
Flexibility High Low
Use case Complex data Simple data

📌 Summary

  • Lists can hold mixed data types

  • Use [[ ]] to extract elements

  • Lists can be nested

  • Very useful for structured data

You may also like...