R Lists
📋 R Lists
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
8. Convert List to Vector (If Possible)
9. Apply Function on List
✔ 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
