Lists in R

 

Lists in R

Introduction

A list is one of the most powerful and flexible data structures in R. Unlike vectors, which can contain only elements of the same type, a list can contain elements of different data types and different structures.

A list can contain:

  • Numbers
  • Characters
  • Logical values
  • Vectors
  • Matrices
  • Arrays
  • Data frames
  • Other lists
  • Functions

Thus, lists are called heterogeneous data structures.


Characteristics of Lists

  • Heterogeneous (elements may be of different types).
  • One-dimensional structure.
  • Elements are ordered.
  • Elements can be named.
  • Can contain other lists (nested lists).
  • Elements are accessed by position or by name.

Creating Lists

Lists are created using the list() function.

Syntax

list(element1, element2, ...)

Example 1: Simple List

student <- list(101, "John", TRUE)

print(student)

Output

[[1]]
[1] 101

[[2]]
[1] "John"

[[3]]
[1] TRUE

Example 2: Named List

student <- list(
RollNo = 101,
Name = "John",
Marks = 95
)

print(student)

Output

$RollNo
[1] 101

$Name
[1] "John"

$Marks
[1] 95

Elements of Different Types

L <- list(
Number = 10,
Name = "Computer",
Status = TRUE,
Marks = c(85,90,95)
)

print(L)

Output

$Number
[1] 10

$Name
[1] "Computer"

$Status
[1] TRUE

$Marks
[1] 85 90 95

Determining List Type

class()

L <- list(10,20)

print(class(L))

Output

[1] "list"

length()

Returns the number of elements.

print(length(L))

Output

[1] 2

Accessing List Elements

There are three ways:

  1. Using [[ ]]
  2. Using [ ]
  3. Using $

Using Double Brackets [[ ]]

Returns the actual element.

L <- list(10,"Hello",TRUE)

print(L[[2]])

Output

[1] "Hello"

Using Single Brackets [ ]

Returns a sublist.

print(L[2])

Output

[[1]]
[1] "Hello"

Notice that the result is still a list.


Using Names

student <- list(
RollNo = 101,
Name = "John",
Marks = 95
)

print(student$Name)

Output

[1] "John"

Equivalent:

print(student[["Name"]])

Output

[1] "John"

Difference Between [ ] and [[ ]]

Consider

L <- list(10,20,30)

Using [ ]

L[2]

Output

[[1]]
[1] 20

Result is a list.

Using [[ ]]

L[[2]]

Output

[1] 20

Result is the actual value.


Adding Elements

L <- list(10,20,30)

L[[4]] <- 40

print(L)

Output

[[1]]
[1] 10

[[2]]
[1] 20

[[3]]
[1] 30

[[4]]
[1] 40

Adding Named Elements

student <- list(
Name = "John",
Marks = 90
)

student$Age <- 20

print(student)

Output

$Name
[1] "John"

$Marks
[1] 90

$Age
[1] 20

Modifying Elements

student$Marks <- 95

print(student)

Output

$Name
[1] "John"

$Marks
[1] 95

$Age
[1] 20

Removing Elements

student$Age <- NULL

print(student)

Output

$Name
[1] "John"

$Marks
[1] 95

Nested Lists

Lists may contain other lists.

student <- list(
Name = "John",
Marks = list(
Maths = 90,
Physics = 95
)
)

print(student)

Output

$Name
[1] "John"

$Marks
$Marks$Maths
[1] 90

$Marks$Physics
[1] 95

Access Nested Elements

print(student$Marks$Physics)

Output

[1] 95

Lists Containing Vectors

L <- list(
Even = c(2,4,6,8),
Odd = c(1,3,5,7)
)

print(L)

Output

$Even
[1] 2 4 6 8

$Odd
[1] 1 3 5 7

Concatenating Lists

L1 <- list(10,20)
L2 <- list(30,40)

L3 <- c(L1,L2)

print(L3)

Output

[[1]]
[1] 10

[[2]]
[1] 20

[[3]]
[1] 30

[[4]]
[1] 40

Converting Vector to List

v <- c(10,20,30)

L <- as.list(v)

print(L)

Output

[[1]]
[1] 10

[[2]]
[1] 20

[[3]]
[1] 30

Converting List to Vector

L <- list(10,20,30)

v <- unlist(L)

print(v)

Output

[1] 10 20 30

Traversing a List

L <- list(10,"Hello",TRUE)

for(i in L)
{
print(i)
}

Output

[1] 10
[1] "Hello"
[1] TRUE

Applying Functions to Lists

L <- list(c(1,2), c(3,4), c(5,6))

result <- lapply(L, sum)

print(result)

Output

[[1]]
[1] 3

[[2]]
[1] 7

[[3]]
[1] 11

Useful Functions for Lists

FunctionPurpose
list()    Create list
length()    Number of elements
class()    Determine class
names()    Get names
unlist()    Convert list to vector
as.list()    Convert vector to list
c()    Combine lists
lapply()    Apply function
str()    Display structure

Example: Student Record

student <- list(
RollNo = 101,
Name = "John",
Marks = c(85,90,95),
Passed = TRUE
)

print(student$Name)

avg <- mean(student$Marks)

cat("Average =", avg)

Output

[1] "John"
Average = 90

Difference Between Vectors and Lists

FeatureVectorList
Data Type    Homogeneous        Heterogeneous
Elements    Same type        Different types
Creation    c()    list()
Nested Structure        No        Yes
Access    [ ]    [ ], [[ ]], $

Applications of Lists

  • Student records
  • Employee databases
  • Hierarchical data
  • Storing model results
  • JSON and XML structures
  • Machine learning outputs

Conclusion

Lists are one of the most versatile data structures in R. They can store heterogeneous data and even other lists, making them suitable for representing complex data structures. Understanding lists is essential because many R functions and statistical models return their results as lists.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Sample Programs to Explore Basic Concepts in R

Basic Input and Output Functions in R