Sample Programs on Lists in R

 

Sample Programs on Lists in R

The following programs illustrate different concepts of lists rather than merely storing records. These examples are suitable for undergraduate laboratories and cover important operations on lists.


Program 1: List Containing Different Data Types

Aim

Create a list containing elements of different data types and access its elements.

Program

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

print(L)

print(L$Name)
print(L[[4]])

Output

$Number
[1] 100

$Name
[1] "Computer Science"

$Status
[1] TRUE

$Marks
[1] 85 90 95

[1] "Computer Science"

[1] 85 90 95

Concept Demonstrated

  • Heterogeneous data
  • Access using $
  • Access using [[ ]]

Program 2: Nested Lists

Aim

Create a nested list and access inner elements.

Program

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

print(student)

print(student$Marks$Physics)

Output

$Name
[1] "John"

$Marks
$Marks$Maths
[1] 90

$Marks$Physics
[1] 95

$Marks$Chemistry
[1] 88

[1] 95

Concept Demonstrated

  • Nested lists
  • Multi-level access

Program 3: Add, Modify and Delete Elements

Aim

Perform insertion, modification and deletion of list elements.

Program

L <- list(10, 20, 30)

# Add element
L[[4]] <- 40

# Modify element
L[[2]] <- 100

# Delete element
L[[1]] <- NULL

print(L)

Output

[[1]]
[1] 100

[[2]]
[1] 30

[[3]]
[1] 40

Concept Demonstrated

  • Dynamic list modification
  • Adding elements
  • Updating elements
  • Removing elements

Program 4: Convert Between Vector and List

Aim

Convert a vector into a list and then convert the list back into a vector.

Program

v <- c(10,20,30,40)

L <- as.list(v)

print(L)

v2 <- unlist(L)

print(v2)

Output

[[1]]
[1] 10

[[2]]
[1] 20

[[3]]
[1] 30

[[4]]
[1] 40

10 20 30 40

Concept Demonstrated

  • as.list()
  • unlist()
  • Conversion between data structures

Program 5: Apply a Function to List Elements

Aim

Find the sum of each vector stored in a list.

Program

L <- list(
c(10,20,30),
c(5,10,15),
c(2,4,6)
)

result <- lapply(L, sum)

print(result)

Output

[[1]]
[1] 60

[[2]]
[1] 30

[[3]]
[1] 12

Concept Demonstrated

  • List traversal
  • Higher-order functions
  • lapply()

Program 6 : List of Functions

Aim

Store functions inside a list and invoke them.

Program

operations <- list(
add = function(a,b) a+b,
multiply = function(a,b) a*b
)

print(operations$add(10,20))

print(operations$multiply(10,20))

Output

[1] 30

[1] 200

Concept Demonstrated

  • Functions as list elements
  • Function invocation through lists

Program 7: Cricket Team Statistics Using Lists

Aim

Create a list containing:

  • Team Name
  • Player Names (vector)
  • Runs scored by each player (vector)

Perform the following operations:

  1. Find the total team score.
  2. Find the highest scorer.
  3. Find the average runs.
  4. Display players who scored more than 50 runs.

Program

# Create a list
team <- list(
TeamName = "India",
Players = c("Rohit", "Gill", "Virat", "Rahul", "Hardik"),
Runs = c(75, 42, 110, 65, 35)
)

# Display team details
print(team)

# Total team score
total_score <- sum(team$Runs)

cat("Total Team Score =", total_score, "\n")

# Highest scorer
max_runs <- max(team$Runs)

highest_scorer <- team$Players[team$Runs == max_runs]

cat("Highest Scorer =", highest_scorer, "\n")
cat("Runs Scored =", max_runs, "\n")

# Average runs
average_runs <- mean(team$Runs)

cat("Average Runs =", average_runs, "\n")

# Players scoring more than 50 runs
good_players <- team$Players[team$Runs > 50]

cat("Players scoring more than 50 runs:\n")
print(good_players)

Output

Total Team Score = 327

Highest Scorer = Virat
Runs Scored = 110

Average Runs = 65.4

Players scoring more than 50 runs:
[1] "Rohit" "Virat" "Rahul"

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