R Program to assign new names to the elements of a given list

How to assign new names to the elements of a given list

Here we are explaining how to write an R program to assign new names to the elements of a given list. Here we are using a built-in function list() for this. This function helps to create a list. The syntax of this function is 

list(…) 

Where ....(dots)are the objects, possibly named.

How to assign new names to the elements of a given list using R program

Below are the steps used in the R program to assign new names to the elements of a given list. In this R program, we directly give the values to a built-in function list(). Here we are using variables L1 for holding the list elements. Call the function list() with different types of elements. The function names() helps to get or set the names of an object.

ALGORITHM

STEP 1: Assign variables L1 with a list

STEP 2: Create L1 with 3 sets of elements g1,g2,g3

STEP 3: Print the original lists

STEP 4: Add a new names A,B,C to the element of the list as  names(L1) = c("A", "B", "C")

STEP 5: Print the final list L1

R Source Code

                                          L1 = list(g1 = 1:5, g2 = "C Programming", g3 = "JAVA")
print("Original list:")
print(L1)
names(L1) = c("A", "B", "C")
print("Assign new names 'A', 'B' and 'C' to the elements of the list")
print(L1)
                                      

OUTPUT

[1] "Original list:"
$g1
[1] 1 2 3 4 5

$g2
[1] "C Programming"

$g3
[1] "JAVA"

[1] "Assign new names 'A', 'B' and 'C' to the elements of the list"
$A
[1] 1 2 3 4 5

$B
[1] "C Programming"

$C
[1] "JAVA"