Skip to content
Related Articles

Related Articles

How to Delete DataFrames in R?

View Discussion
Improve Article
Save Article
  • Last Updated : 19 Dec, 2021
View Discussion
Improve Article
Save Article

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 sis 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)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!