📜  使用 Dplyr 在 R 中选择变量(列)

📅  最后修改于: 2022-05-13 01:55:46.353000             🧑  作者: Mango

使用 Dplyr 在 R 中选择变量(列)

在本文中,我们将使用 dplyr 库在 R 编程语言中选择变量或列。

使用中的数据集:

选择带有列名的列

这里我们将使用 select() 方法按名称选择列

句法:



这里,数据框是输入数据框,列是要显示的数据框中的列

示例 1: 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'))
  
# select id column from the dataframe by 
# column name
print(select(data1,id))
    
# select name column from the dataframe by
# column name
print(select(data1,name))


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'))
  
# select multiple columns from the dataframe
# by column name
print(select(data1,id,name,address))


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'))
  
# select first column by column position
print(select(data1,1))
  
# select third column by column 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'))
  
# select multiple column by column position
print(select(data1,1,2))


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'))
  
# select multiple column by column 
# position with : operator
print(select(data1,1: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'))
  
# select column that contains am
print(select(data1,contains('am')))
  
# select column that contains d
print(select(data1,contains('d')))
  
# select column that contains dd
print(select(data1,contains('dd')))


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'))
  
# select column that matches with  am
print(select(data1,matches('am')))
  
# select column that matches with d
print(select(data1,matches ('d')))
  
# select column that matches with  dd
print(select(data1,matches ('dd')))


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'))
  
# select column that starts with n
print(select(data1,starts_with('n')))
  
# select column that starts with add
print(select(data1,starts_with('add')))


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'))
  
# select column that ends with ss
print(select(data1,ends_with('ss')))
  
# select column that ends with d
print(select(data1,ends_with('d')))


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'))
  
# select all columns using everything method
print(select(data1,everything()))


输出:

示例 2 :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'))
  
# select multiple columns from the dataframe
# by column name
print(select(data1,id,name,address))

输出:

按位置选择列

我们还可以使用列位置并使用 select() 方法获取列。位置从 1 开始。

句法:

为了选择多列,我们可以使用范围运算符“;”按位置选择列

句法:

示例 1: 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'))
  
# select first column by column position
print(select(data1,1))
  
# select third column by column position
print(select(data1,3))
   

输出:

示例2: 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'))
  
# select multiple column by column position
print(select(data1,1,2))

输出:

示例 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'))
  
# select multiple column by column 
# position with : operator
print(select(data1,1:3))

输出:

选择包含值或匹配模式的列

在这里,我们将根据列中存在的值或模式显示列值

方法一:使用 contains()

显示包含给定子字符串的列

句法:

这里,dataframe 是输入数据帧,sub_string 是列名中存在的字符串

示例: 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'))
  
# select column that contains am
print(select(data1,contains('am')))
  
# select column that contains d
print(select(data1,contains('d')))
  
# select column that contains dd
print(select(data1,contains('dd')))

输出:



方法二:使用matches()

它将检查并显示包含给定子字符串的列

这里,dataframe 是输入数据帧,sub_string 是列名中存在的字符串

示例: 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'))
  
# select column that matches with  am
print(select(data1,matches('am')))
  
# select column that matches with d
print(select(data1,matches ('d')))
  
# select column that matches with  dd
print(select(data1,matches ('dd')))

输出:

选择以特定字符开头或结尾的列

在这里我们还可以根据开始和结束字符选择列。

  • starts_with()用于返回以给定字符开头的列。

句法:



  • Ends_with()用于返回以给定字符结尾的列。

句法:

示例 1: R 程序显示以字符/substring 开头的列

电阻

# 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'))
  
# select column that starts with n
print(select(data1,starts_with('n')))
  
# select column that starts with add
print(select(data1,starts_with('add')))

输出:

示例 2: 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'))
  
# select column that ends with ss
print(select(data1,ends_with('ss')))
  
# select column that ends with d
print(select(data1,ends_with('d')))

输出:

选择所有列

我们可以使用everything() 方法选择数据框中的所有列。

语法

示例: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'))
  
# select all columns using everything method
print(select(data1,everything()))

输出: