The Wayback Machine - https://web.archive.org/web/20240831182807/https://www.geeksforgeeks.org/how-to-delete-dataframes-in-r/
Open In App

How to Delete DataFrames in R?

Last Updated : 06 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will discuss how to delete Dataframes in R Programming Language. A DataFrame is a two-dimensional data structure that can store data in rows and columns, we can create a dataframe by using data.frame() function

Create Dataframe for demonstration:

R




# dataframe1
data1 = data.frame(names=c("sravan","ojaswi"),
                   address=c("delhi","hyd"))
 
# dataframe2
data2 = data.frame(names=c("sravan","ojaswi"),
                   marks=c(98,90))
 
# dataframe3
data3 = data.frame(names=c("sravan","ojaswi"),
                   age=c(23,17))
 
# display
print(data1)
print(data2)
print(data3)


Output:

   names address
1 sravan   delhi
2 ojaswi     hyd
   names marks
1 sravan    98
2 ojaswi    90
   names age
1 sravan  23
2 ojaswi  17

Before deleting the dataframe we have to check how many and what are the dataframes exists, we can get this by using ls() function. This will return the current variables that exists in an environment

Syntax: ls()

Let’s check:

R




#dataframe1
data1 = data.frame(names=c("sravan","ojaswi"),
                   address=c("delhi","hyd"))
 
# dataframe2
data2 = data.frame(names=c("sravan","ojaswi"),
                   marks=c(98,90))
 
# dataframe3
data3 = data.frame(names=c("sravan","ojaswi"),
                   age=c(23,17))
 
ls()


Output:

[1] "data1" "data2" "data3"

Method 1: Using rm() methods

This method stands for remove. This method will remove the given dataframe

Syntax: rm(dataframe)

where dataframe is the name of the existing dataframe

Example: R program to create three dataframes and delete two dataframes

R




# dataframe1
data1 = data.frame(names=c("sravan","ojaswi"),
                   address=c("delhi","hyd"))
 
# dataframe2
data2 = data.frame(names=c("sravan","ojaswi"),
                   marks=c(98,90))
 
# dataframe3
data3 = data.frame(names=c("sravan","ojaswi"),
                   age=c(23,17))
 
# deleet dataframe1
rm(data1)
 
# deleet dataframe2
rm(data2)
 
# display
ls()


Output:

[1] "data3"

We can also delete multiple dataframes separated by comma using rm() function:

Syntax: rm(“dataframe1″,”datafame2″,……,”dataframe n”)

R




# dataframe1
data1 = data.frame(names=c("sravan","ojaswi"),
                   address=c("delhi","hyd"))
 
# dataframe2
data2 = data.frame(names=c("sravan","ojaswi"),
                   marks=c(98,90))
 
# dataframe3
data3 = data.frame(names=c("sravan","ojaswi"),
                   age=c(23,17))
 
# delete dataframe1, dataframe2
rm("data1","data2")
 
# display
ls()


Output:

[1] "data3"

We can also delete all dataframes by using sapply()

Syntax: rm(list=ls(all=TRUE)[sapply(mget(ls(all=TRUE)), class) == “data.frame”])

R




# dataframe1
data1= data.frame(names=c("sravan","ojaswi"),
                  address=c("delhi","hyd"))
 
# dataframe2
data2 = data.frame(names=c("sravan","ojaswi"),
                   marks=c(98,90))
 
# dataframe3
data3 = data.frame(names=c("sravan","ojaswi"),
                   age=c(23,17))
 
# delete all dataframes
rm(list=ls(all=TRUE)[sapply(mget(ls(all=TRUE)),
                            class) == "data.frame"])
 
# display
ls()


Output:

character(0)

Method 2: Using grepl() function

This function will delete all the dataframes existing in the current environment used with rm().

Syntax: rm(list = ls()[grepl(“pattern”, ls())])

where, pattern is all dataframes starting letter variables

Example:

R




# dataframe1
data1 = data.frame(names=c("sravan","ojaswi"),
                   address=c("delhi","hyd"))
 
# dataframe2
data2 = data.frame(names=c("sravan","ojaswi"),
                   marks=c(98,90))
 
# dataframe3
data3 = data.frame(names=c("sravan","ojaswi"),
                   age=c(23,17))
 
# delete all dataframes
rm(list = ls()[grepl("data", ls())])
 
# display
ls()


Output:

character(0)


Similar Reads

List of Dataframes in R
DataFrames are generic data objects of R which are used to store the tabular data. They are two-dimensional, heterogeneous data structures. A list in R, however, comprises of elements, vectors, data frames, variables, or lists that may belong to different data types. In this article, we will study how to create a list consisting of data frames as i
8 min read
Joining of Dataframes in R Programming
rreIn R Language, dataframes are generic data objects which are used to store the tabular data. Dataframes are considered to be the most popular data objects in R programming because it is more comfortable to analyze the data in the tabular form. Dataframes can also be taught as mattresses where each column of a matrix can be of the different data
7 min read
Reading the CSV file into Dataframes in R
In this article, we will learn how to import or read a CSV file into a dataframe in R Programming Language. Data set in use: Step 1: Set or change the working directory In order to import or read the given CSV file into our data frame, we first need to check our current working directory, and make sure that the CSV file is in the same directory as
3 min read
How to merge dataframes in R ?
In this article, we will discuss how to perform inner, outer, left, or right joins in a given dataframe in R Programming Language. Functions Used merge() function is used to merge or join two tables. With appropriate values provided to specific parameters, we can create the desired join. Syntax: merge(df1, df2, by.df1, by.df2, all.df1, all.df2, sor
3 min read
How to Export Multiple Dataframes to Different Excel Worksheets in R
In this article, we are going to see how to export multiple dataframe to different Excel Worksheets in R Programming Language. We will be using the xlsx module. The xlsx library provides R functions to read/write/format Excel files and their formats. The xlsx package gives programmatic control of Excel files. The modification of the sheet's compone
4 min read
How to Loop Through Column Names in R dataframes?
In this article, we will discuss how to loop through column names in dataframe in R Programming Language. Method 1: Using sapply() Here we are using sapply() function with some functions to get column names. This function will return column names with some results Syntax: sapply(dataframe,specific function) where dataframe is the input dataframespe
2 min read
Plot columns from list of dataframes in R
In this article, we will discuss how to plot columns from a list of dataframes in R programming language. Note: We are taking a line plot for implementation same can be applied to any other plot. The ggplot() method is supplemented by different geometrical shapes to indicate the type of data plotting. The geom_line() method is used to plot the data
2 min read
Intersection of dataframes using Dplyr in R
In this article, we will discuss how to find the Intersection of two dataframes using the Dplyr package in R programming language. Dplyr provides intersect() method to get the common data in two dataframes. Syntax: intersect(dataframe1,dataframe2,dataframe3,........,dataframe n) We can perform this operation on the two dataframes. Example 1: R prog
1 min read
Get difference of dataframes using Dplyr in R
In this article, we will discuss How to find the difference between two dataframes using the Dplyr package in the R programming language. Set difference refers to getting or extracting those values from one dataset that are not present in the other. For this, dplyr supports a function called setdiff(). setdiff() is used to return the data present i
1 min read
Plot lines from a list of dataframes using ggplot2 in R
In this article, we will discuss how to plotlines in ggplot2 from a list of data frames in the R programming language. The ggplot2 package is used to visualize and analyze the data. The package can be downloaded and installed using the following command in R : install.packages("ggplot2")The ggplot2 method in R is used to do graph visualizations usi
5 min read
How to Merge DataFrames Based on Multiple Columns in R?
In this article, we will discuss how to merge dataframes based on multiple columns in R Programming Language. We can merge two dataframes based on multiple columns by using merge() function Syntax: merge(dataframe1, dataframe2, by.x=c('column1', 'column2'...........,'column n'), by.y=c('column1', 'column2'...........,'column n')) where dataframe1 i
2 min read
Export Dataframes to Multiple Excel Sheets in R
An excel workbook is used to contain tabular information stored within a cell-like structure. Every excel workbook in R contains multiple sheets to contain the data belonging to the same domain information. The excel sheet can contain columns belonging to different data types. The Excel files can be created from data frames using R. Using xlsx libr
5 min read
Delete rows with empty cells from Excel using R
Practically during any survey for data collection, getting all the information from all units is not possible as sometimes we get partial information and sometimes nothing. Therefore, it is possible that some rows in our data are completely blank and some might have partial data. The blank rows can be removed and the other empty values can be fille
1 min read
How to Delete a File using R
In this article, we will discuss how to delete a file in R Programming Language. Directory in use: Method 1: Using file.remove() This is the simplest approach to delete a file as in this approach the user just needs to call the file.remove() function which is a bases function of the R programming language and in this function, the user just has to
2 min read
Create, modify, and delete columns using dplyr package in R
In this article, we will discuss mutate function present in dplyr package in R Programming Language to create, modify, and delete columns of a dataframe. Create new columns Columns can be inserted either by appending a new column or using existing columns to evaluate a new column. By default, columns are added to the far right. Although columns can
3 min read
How to Delete a Row by Reference in data.table in R?
In R Language the data.table package is highly efficient for data manipulation, especially for large datasets. One of its powerful features is the ability to modify data by reference, which avoids copying the entire dataset and thus improves performance. This article will guide you through the process of deleting rows by reference in data.table usi
3 min read
How Do You Delete a Column by Name in data.table in R?
Data manipulation is a critical aspect of the data analysis and R's data.table package is a powerful tool for handling large datasets efficiently. One common task is deleting a column by its name. This article will guide us through the process providing examples and best practices to ensure we can manage the data tables effectively. What is a data.
3 min read
Delete Rows Containing Specific Strings in R
When working with datasets in R, you may encounter situations where you need to filter out rows that contain specific strings in one or more columns. This can be particularly useful for cleaning data, removing outliers, or excluding certain categories from analysis. In this article, we will explore different methods to delete rows containing specif
4 min read
Change column name of a given DataFrame in R
A data frame is a tabular structure with fixed dimensions, of each rows as well as columns. It is a two-dimensional array like object with numerical, character based or factor-type data. Each element belonging to the data frame is indexed by a unique combination of the row and column number respectively. Column names are addressed by unique names.
5 min read
R Tutorial | Learn R Programming Language
R Programming Tutorial is designed for beginners and experts. This free R Tutorial gives you knowledge basic to advanced of concepts of the R programming language. Here you will get a detailed introduction, features, installation, variables, data types, operators, if statements, vectors, data handling, graphics, and statistical modeling of R progra
9 min read
R - Data Frames
R Programming Language is an open-source programming language that is widely used as a statistical software and data analysis tool. Data Frames in R Language are generic data objects of R that are used to store tabular data.  Data frames can also be interpreted as matrices where each column of a matrix can be of different data types. R DataFrame is
10 min read
Taking Input from User in R Programming
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. Like other programming languages in R it's also possible to take input from the user. For doing so, there are two methods in R. Using readline() me
7 min read
Printing Output of an R Program
In R there are various methods to print the output. Most common method to print output in R program, there is a function called print() is used. Also if the program of R is written over the console line by line then the output is printed normally, no need to use any function for print that output. To do this just select the output variable and pres
4 min read
Read contents of a CSV File in R Programming - read.csv() Function
read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. Syntax: read.csv(file, header, sep, dec) Parameters: file: the path to the file containing the data to be imported into R. header: logical value. If TRUE, read.csv() assumes that your file has a header row, so row 1 is the n
3 min read
R - Matrices
R-matrix is a two-dimensional arrangement of data in rows and columns. In a matrix, rows are the ones that run horizontally and columns are the ones that run vertically. In R programming, matrices are two-dimensional, homogeneous data structures. These are some examples of matrices: Creating a Matrix in RTo create a matrix in R you need to use the
12 min read
R Data Types
Different forms of data that can be saved and manipulated are defined and categorized using data types in computer languages, including R. Each R data type has unique properties and associated operations. What are R Data types?R Data types are used to specify the kind of data that can be stored in a variable. For effective memory consumption and pr
8 min read
Getting started with Data Visualization in R
Data visualization is the technique used to deliver insights in data using visual cues such as graphs, charts, maps, and many others. This is useful as it helps in intuitive and easy understanding of the large quantities of data and thereby make better decisions regarding it. Data Visualization in R Programming LanguageThe popular data visualizatio
6 min read
R Operators
Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. R Operators R supports majorly four kinds of binary operators between a set of
9 min read
R Programming Language - Introduction
The R Language stands out as a powerful tool in the modern era of statistical computing and data analysis. Widely embraced by statisticians, data scientists, and researchers, the R Language offers an extensive suite of packages and libraries tailored for data manipulation, statistical modeling, and visualization. In this article, we explore the fea
7 min read
R - Bar Charts
Bar charts are a popular and effective way to visually represent categorical data in a structured manner. R stands out as a powerful programming language for data analysis and visualization. In this article, we'll look at how to make visually appealing bar charts in R. Bar Charts using RA bar chart also known as bar graph is a pictorial representat
5 min read
Article Tags :