R Program to extract elements of a list

November 29, 2021, Learn eTutorial

How to extract all elements except the 3rd one of the first vectors of a list

To extract elements from a list in R programming, we are using the list() function. It is a built-in function that helps to create a list. The syntax of this function is 



  • .... (dots) are the objects.

How to selectively extract specific elements from a list in R programming?

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 g1, g2, and g3. Call the function list() with different types of elements. Extract all elements except the 3rd one of the first vector as like L1$g1 = L1$g1[-3]. Finally, print the vector value.


STEP 1: Assign variables L1 with a list

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

STEP 3: Print the first vector

STEP 4: The first vector without the element is extracted by  L1$g1 = L1$g1[-3]

STEP 5: Print the final vector value

R Source Code

                                          L1 = list(g1 = 1:5, g2 = "C Programming", g3 = "JAVA")
print("Original list:")
print("First vector:")
print("First vector without third element:")
L1$g1 = L1$g1[-3]


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

[1] "C Programming"

[1] "JAVA"

[1] "First vector:"
[1] 1 2 3 4 5
[1] "First vector without third element:"
[1] 1 2 4 5