Slip10
Write a script in R to create a list of cities and perform the following
1) Give names to the elements in the list.
2) Add an element at the end of the list.
3) Remove the last element.
4) Update the 3rd Element
print(" 1. Give names to the elements in the list.")
list_data <-
list(c("A","B","C"),list("Alibag",
"Mumbai", "Delhi"))
print(list_data)
names(list_data) = c("Alphabets", "Cities(s)")
print("List with column names:")
print(list_data)
print("2. Add an element at the end of the list.")
list_data <-
list(c("A","B","C"),list("Alibag",
"Mumbai", "Delhi"))
print("List:")
print(list_data)
print("Add a new element at the end of the list:")
list_data[4] = "Pune"
print("New list:")
print(list_data)
print("3. Remove the last element.")
list_data <-
list(c("A","B","C"),list("Alibag",
"Mumbai", "Delhi"))
print(list_data)
print("Remove the Last element of the list:")
list_data[2] = NULL
print("New list:")
print(list_data)
print("4. Update the 3rd Element")
list_data <-
list(c("A","B","C"),list("Alibag"),list("Neha"))
print(list_data)
print("Update the second element of the list:")
list_data[3] = "Big Data"
print("New list:")
print(list_data)
OutPut
> print(" 1. Give names to the elements in the list.")
[1] " 1. Give names to the elements in the list."
> list_data <-
list(c("A","B","C"),list("Alibag",
"Mumbai", "Delhi"))
> print(list_data)
[[1]]
[1] "A" "B" "C"
[[2]]
[[2]][[1]]
[1] "Alibag"
[[2]][[2]]
[1] "Mumbai"
[[2]][[3]]
[1] "Delhi"
> names(list_data) = c("Alphabets", "Cities(s)")
>
>
> print("List with column names:")
[1] "List with column names:"
> print(list_data)
$Alphabets
[1] "A" "B" "C"
$`Cities(s)`
$`Cities(s)`[[1]]
[1] "Alibag"
$`Cities(s)`[[2]]
[1] "Mumbai"
$`Cities(s)`[[3]]
[1] "Delhi"
> print("2. Add an element at the end of the list.")
[1] "2. Add an element at the end of the list."
> list_data <-
list(c("A","B","C"),list("Alibag",
"Mumbai", "Delhi"))
> print("List:")
[1] "List:"
> print(list_data)
[[1]]
[1] "A" "B" "C"
[[2]]
[[2]][[1]]
[1] "Alibag"
[[2]][[2]]
[1] "Mumbai"
[[2]][[3]]
[1] "Delhi"
> print("Add a new element at the end of the list:")
[1] "Add a new element at the end of the list:"
> list_data[4] = "Pune"
> print("New list:")
[1] "New list:"
> print(list_data)
[[1]]
[1] "A" "B" "C"
[[2]]
[[2]][[1]]
[1] "Alibag"
[[2]][[2]]
[1] "Mumbai"
[[2]][[3]]
[1] "Delhi"
[[3]]
NULL
[[4]]
[1] "Pune"
>
>
> print("3. Remove the last element.")
[1] "3. Remove the last element."
> list_data <-
list(c("A","B","C"),list("Alibag",
"Mumbai", "Delhi"))
> print(list_data)
[[1]]
[1] "A" "B" "C"
[[2]]
[[2]][[1]]
[1] "Alibag"
[[2]][[2]]
[1] "Mumbai"
[[2]][[3]]
[1] "Delhi"
> print("Remove the Last element of the list:")
[1] "Remove the Last element of the list:"
> list_data[2] = NULL
> print("New list:")
[1] "New list:"
> print(list_data)
[[1]]
[1] "A" "B" "C"
>
> print("4. Update the 3rd Element")
[1] "4. Update the 3rd Element"
> list_data <-
list(c("A","B","C"),list("Alibag"),list("Neha"))
> print(list_data)
[[1]]
[1] "A" "B" "C"
[[2]]
[[2]][[1]]
[1] "Alibag"
[[3]]
[[3]][[1]]
[1] "Neha"
> print("Update the second element of the list:")
[1] "Update the second element of the list:"
> list_data[3] = "Big Data"
> print("New list:")
[1] "New list:"
> print(list_data)
[[1]]
[1] "A" "B" "C"
[[2]]
[[2]][[1]]
[1] "Alibag"
[[3]]
[1] "Big Data"