Remove Element From List in R Language - GeeksforGeeks

21 Apr, 2021
R is an open-source programming language that is widely used as a statistical software and data analysis tool. R generally comes with the Command-line interface. R is available across widely used platforms like Windows, Linux, and macOS. Also, the R programming language is the latest cutting-edge tool. This article specifies how to remove one or more elements from a given list in R language. It will specify the number of elements to be removed from the list and print the rest elements. As a result, it will return a list of data after performing the operation. You will learn how to remove certain elements from the list.
Syntax: To remove elements using minus sign
my_list[- number]   
Parameters: 
number – Specifies the number of elements to be removed from the list 
Return:
Return list after removing elements 
Approach
Create a vector.
Remove one specific element using the (-) method.
Delete multiple elements using the same method as illustrated in examples.
Example: 
R
# Create example listmy_list <- list(a = c(1, 4, 2, 7),                                 b = "geeksforgeeks",                c = 2)my_list  # remove elements my_list[- 1]
Output:
Here, in the above example, my_list[- 1] will remove the data stored in the first list, and after removal of the first data list print the rest data on the screen.
Syntax:
my_list_2[number] <- NULL  
Example:
R
my_list <- list(a = c(1, 4, 2, 7),                 # Create example list                b = "geeksforgeeks",                c = 2)my_listmy_list_2 <- my_list                               # Replicate listmy_list_2[1] <- NULL                               # Remove list elements with NULLmy_list_2
Output :
The above example will remove elements by making them NULL in the list by using the my_list_2[] <- NULL   method. It will clear specific data and print the rest data on the screen.
Syntax: 
my_list[names(my_list) %in% "number" == FALSE]    
Example:
R
# Create example listmy_list <- list(a = c(1, 4, 2, 7),                                 b = "geeksforgeeks",                c = 2)my_list  # Remove elementsmy_list[names(my_list) %in% "c" == FALSE]
Output:
my_list[names(my_list) %in% “c” == FALSE], with this method the list of data which are declared as false, which get removed from the data list and other will be printed on the screen.
Syntax: 
my_list[names(my_list) != "number"]
Example:
R
# Create example listmy_list <- list(a = c(1, 4, 2, 7),                                b = "geeksforgeeks",                c = 2)my_listmy_list[names(my_list) != "a"]
Output: 
my_list[names(my_list) != ” ”  In above code the data list number which is entered between double quote will get  removed .
Example:  
R
# Create example listmy_list <- list(a = c(1, 4, 2, 7),                                 b = "geeksforgeeks",                c = 2)my_listmy_list[- c(1, 3)]
Output:
R-List
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
R - if statement
How to import an Excel File into R ?
How to change the order of bars in bar chart in R ?