如何使用 R 中的 Dplyr 包按名称和索引删除列
在本文中,我们将使用 dplyr 包在 R 编程语言中按名称和索引删除列。
使用中的数据集:
使用列名删除列
我们可以通过列名使用 select() 方法删除列。
语法:
select(dataframe,-column_name)
其中,dataframe 是输入数据帧,column_name 是要删除的列的名称。
示例:删除列的 R 程序
R
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
name=c('sravan','ojaswi','bobby',
'gnanesh','rohith','pinkey',
'dhanush','sravan','gnanesh',
'ojaswi'),
address=c('hyd','hyd','ponnur','tenali',
'vijayawada','vijayawada','guntur',
'hyd','tenali','hyd'))
# remove name column
print(select(data1,-name))
# remove id column
print(select(data1,-id))
R
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
name=c('sravan','ojaswi','bobby',
'gnanesh','rohith','pinkey',
'dhanush','sravan','gnanesh',
'ojaswi'),
address=c('hyd','hyd','ponnur','tenali',
'vijayawada','vijayawada','guntur',
'hyd','tenali','hyd'))
# remove name and id column
print(select(data1,-c(id,name)))
# remove name and address column
print(select(data1,-c(address,name)))
# remove all column
print(select(data1,-c(address,name,id)))
R
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
name=c('sravan','ojaswi','bobby',
'gnanesh','rohith','pinkey',
'dhanush','sravan','gnanesh',
'ojaswi'),
address=c('hyd','hyd','ponnur','tenali',
'vijayawada','vijayawada','guntur',
'hyd','tenali','hyd'))
# remove name column by its position
print(select(data1,-2))
# remove address column by its position
print(select(data1,-3))
R
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
name=c('sravan','ojaswi','bobby',
'gnanesh','rohith','pinkey',
'dhanush','sravan','gnanesh',
'ojaswi'),
address=c('hyd','hyd','ponnur','tenali',
'vijayawada','vijayawada','guntur',
'hyd','tenali','hyd'))
# remove name and id columns by
# its position
print(select(data1,-c(1,2)))
输出:
使用列名删除多列
我们可以通过列名使用 select() 方法删除列
语法:
select(dataframe,-c(column_name1,column_name2,.,column_name n)
其中,dataframe 是输入数据帧,-c(column_names) 是要删除的列名称的集合。
示例: R 程序删除多列
电阻
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
name=c('sravan','ojaswi','bobby',
'gnanesh','rohith','pinkey',
'dhanush','sravan','gnanesh',
'ojaswi'),
address=c('hyd','hyd','ponnur','tenali',
'vijayawada','vijayawada','guntur',
'hyd','tenali','hyd'))
# remove name and id column
print(select(data1,-c(id,name)))
# remove name and address column
print(select(data1,-c(address,name)))
# remove all column
print(select(data1,-c(address,name,id)))
输出:
使用列索引删除列
我们可以通过列索引/位置使用 select() 方法删除列。索引从 1 开始。
句法:
select(dataframe,-column_index)
其中,dataframe 是输入数据帧,column_index 是要删除的列的位置。
示例:删除特定列的 R 程序
电阻
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
name=c('sravan','ojaswi','bobby',
'gnanesh','rohith','pinkey',
'dhanush','sravan','gnanesh',
'ojaswi'),
address=c('hyd','hyd','ponnur','tenali',
'vijayawada','vijayawada','guntur',
'hyd','tenali','hyd'))
# remove name column by its position
print(select(data1,-2))
# remove address column by its position
print(select(data1,-3))
输出:
使用列索引删除多列
我们可以通过列索引/位置使用 select() 方法删除列。索引从 1 开始。
句法:
select(dataframe,-c(column_index1,column_index2,.,column_index n)
其中,dataframe 是输入数据帧,c(column_indexes) 是要删除的列的位置。
示例: R 程序按位置删除多列
电阻
# load the library
library(dplyr)
# create dataframe with 3 columns
# id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
name=c('sravan','ojaswi','bobby',
'gnanesh','rohith','pinkey',
'dhanush','sravan','gnanesh',
'ojaswi'),
address=c('hyd','hyd','ponnur','tenali',
'vijayawada','vijayawada','guntur',
'hyd','tenali','hyd'))
# remove name and id columns by
# its position
print(select(data1,-c(1,2)))
输出: