如何删除 R 中的数据框?
在本文中,我们将讨论如何在 R 编程语言中删除数据框。 DataFrame 是一种二维数据结构,可以将数据存储在行和列中,我们可以使用 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))
# display
print(data1)
print(data2)
print(data3)
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()
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()
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()
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()
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()
输出:
names address
1 sravan delhi
2 ojaswi hyd
names marks
1 sravan 98
2 ojaswi 90
names age
1 sravan 23
2 ojaswi 17
在删除数据框之前,我们必须检查存在多少数据框以及存在哪些数据框,我们可以使用 ls()函数来获取它。这将返回环境中存在的当前变量
Syntax: ls()
让我们检查:
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()
输出:
[1] "data1" "data2" "data3"
方法一:使用 rm() 方法
此方法代表删除。此方法将删除给定的数据框
Syntax: rm(dataframe)
where dataframe is the name of the existing dataframe
示例: R 程序创建三个数据框并删除两个数据框
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()
输出:
[1] "data3"
我们还可以使用 rm()函数删除多个以逗号分隔的数据帧:
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()
输出:
[1] "data3"
我们还可以使用 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()
输出:
character(0)
方法二:使用 grepl()函数
此函数将删除与 rm() 一起使用的当前环境中存在的所有数据帧。
Syntax: rm(list = ls()[grepl(“pattern”, ls())])
where, pattern sis all dataframes starting letter variables
例子:
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()
输出:
character(0)