Here we are explaining how to write an R program to add a new item to a given list. Here we are using the given list for adding new items. The use of the $ symbol helps to specify the new item along with the list.
– where x indicates the position or element of the list df.
Below are the steps used in the R program to add a new item to a given list. In this R program, we directly give the values to a list by specifying the element with the position. Consider the variable L1 which holds the list elements. And here adding a new element to the position x4.
L1 = list(x1 = 1:5, x2 = "Apple", x3 = "Mango")
print("Original list:")
## [1] "Original list:"
print(L1)
## $x1
## [1] 1 2 3 4 5
##
## $x2
## [1] "Apple"
##
## $x3
## [1] "Mango"
print("Add a new vector to the list:")
## [1] "Add a new vector to the list:"
L1$x4 = "Banana"
print(L1)
## $x1
## [1] 1 2 3 4 5
##
## $x2
## [1] "Apple"
##
## $x3
## [1] "Mango"
##
## $x4
## [1] "Banana"