Thursday 12 October 2023

Bigdata-Slip29-Write a script in R to create a list of students and perform the following 1) Give names to the students in the list. 2) Add a student at the end of the list. 3) Remove the firstStudent. 4) Update the second last student.

 

Slip29

Write a script in R to create a list of students and perform the following

1) Give names to the students in the list.

2) Add a student at the end of the list.

3) Remove the firstStudent.

4) Update the second last student.

 

 # Create a list of students

students <- list("Student1", "Student2", "Student3", "Student4", "Student5")

 

# Display the initial list

cat("Initial List of Students:\n")

print(students)

 

# 1) Give names to the students in the list

students <- c("Alice", "Bob", "Charlie", "David", "Eve")

 

# 2) Add a student at the end of the list

students <- c(students, "Frank")

 

# Display the updated list

cat("\nList of Students After Adding 'Frank':\n")

print(students)

 

# 3) Remove the first student

students <- students[-1]

 

# Display the list after removing the first student

cat("\nList of Students After Removing the First Student:\n")

print(students)

 

# 4) Update the second last student

students[length(students) - 1] <- "George"

 

# Display the list after updating the second last student

cat("\nList of Students After Updating the Second Last Student:\n")

print(students)

 

Output

 

> # Create a list of students

>

> students <- list("Student1", "Student2", "Student3", "Student4", "Student5")

 

> # Display the initial list

>

> cat("Initial List of Students:\n")

Initial List of Students:

>

> print(students)

[[1]]

[1] "Student1"

 

[[2]]

[1] "Student2"

 

[[3]]

[1] "Student3"

 

[[4]]

[1] "Student4"

 

[[5]]

[1] "Student5"

 

>

>

>

> # 1) Give names to the students in the list

>

> students <- c("Alice", "Bob", "Charlie", "David", "Eve")

>

>

>

> # 2) Add a student at the end of the list

>

> students <- c(students, "Frank")

>

>

>

> # Display the updated list

>

> cat("\nList of Students After Adding 'Frank':\n")

 

List of Students After Adding 'Frank':

>

> print(students)

[1] "Alice"   "Bob"     "Charlie" "David"   "Eve"     "Frank" 

>

>

>

> # 3) Remove the first student

>

> students <- students[-1]

>

>

>

> # Display the list after removing the first student

>

> cat("\nList of Students After Removing the First Student:\n")

 

List of Students After Removing the First Student:

>

> print(students)

[1] "Bob"     "Charlie" "David"   "Eve"     "Frank" 

>

>

>

> # 4) Update the second last student

>

> students[length(students) - 1] <- "George"

>

>

>

> # Display the list after updating the second last student

>

> cat("\nList of Students After Updating the Second Last Student:\n")

 

List of Students After Updating the Second Last Student:

>

> print(students)

[1] "Bob"     "Charlie" "David"   "George"  "Frank" 

>