B.Sc Computer Science
R Programming - Lab
Bharathiar University
Program 5
List and Operators
Program 5- Write a R program to perform various operations on lists
SOURCE CODE:
# R program for lists
# Create a list
rollno <- c(10,11,12)
name <- c("ram","rocky","raj")
marks <- c(98,99,80)
rank <- c('B','A','C')
student <- list(rollno, name, marks, rank)
# Print the original list
print("Original List:")
print(student)
# naming the original list
print("Original List with names:")
names(student) <- c("Roll number", "Name", "Marks", "Rank")
print(student)
# Accessing elements in the list
print("Accessing third element:")
print(student[3])
# Accessing fourth element with name in the list
print("Accessing fourth element with name:")
print(student$Rank)
# Adding an element to the list
print("Adding an element:")
student[5] <- "Bsc IT"
print(student)
# updating an element to the list
print("updating last element:")
student[5] <- "Bsc CS"
print(student)
# Calculating the length of the list
print("Length of the list:")
student_length <- length(student)
print(student_length)
# Removing an element from the list
print("Removing an element:")
student <- student[-5] # Remove the fifth element
print(student)
Sample Output:
> # Create a list
> rollno <- c(10,11,12)
> name <- c("ram","rocky","raj")
> marks <- c(98,99,80)
> rank <- c('B','A','C')
> student <- list(rollno, name, marks, rank)
> > # Print the original list
> print("Original List:")
[1] "Original List:" > print(student)
[[1]] [1] 10 11 12 [[2]] [1] "ram" "rocky" "raj" [[3]] [1] 98 99 80 [[4]] [1] "B" "A" "C" > > # naming the original list
> print("Original List with names:")
[1] "Original List with names:" > names(student) <- c("Roll number", "Name", "Marks", "Rank")
> print(student)
$`Roll number` [1] 10 11 12 $Name [1] "ram" "rocky" "raj" $Marks [1] 98 99 80 $Rank [1] "B" "A" "C" > > > # Accessing elements in the list
> print("Accessing third element:")
[1] "Accessing third element:" > print(student[3])
$Marks [1] 98 99 80 > > # Accessing fourth element with name in the list
> print("Accessing fourth element with name:")
[1] "Accessing fourth element with name:" > print(student$Rank)
[1] "B" "A" "C" > > > # Adding an element to the list
> print("Adding an element:")
[1] "Adding an element:" > student[5] <- "Bsc IT"
> print(student)
$`Roll number` [1] 10 11 12 $Name [1] "ram" "rocky" "raj" $Marks [1] 98 99 80 $Rank [1] "B" "A" "C" [[5]] [1] "Bsc IT" > > # updating an element to the list
> print("updating last element:")
[1] "updating last element:" > student[5] <- "Bsc CS"
> print(student)
$`Roll number` [1] 10 11 12 $Name [1] "ram" "rocky" "raj" $Marks [1] 98 99 80 $Rank [1] "B" "A" "C" [[5]] [1] "Bsc CS" > > # Calculating the length of the list
> print("Length of the list:")
[1] "Length of the list:" > student_length <- length(student)
> print(student_length)
[1] 5 > > > # Removing an element from the list
> print("Removing an element:")
[1] "Removing an element:" > student <- student[-5] # Remove the fifth element
> print(student)
$`Roll number` [1] 10 11 12 $Name [1] "ram" "rocky" "raj" $Marks [1] 98 99 80 $Rank[1] "B" "A" "C" |
|
|
No comments:
Post a Comment