📜  使用 Dplyr 重命名 R 中的列名

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

使用 Dplyr 重命名 R 中的列名

在本文中,我们将使用 R 编程语言中的 dplyr 包重命名列名。

使用中的数据集:

方法 1:使用 rename()

此方法用于重命名数据框中的列

句法:



其中 dataframe 是输入数据帧,new_columnname 是列的新名称,old_columnname 是列的旧名称。

示例:重命名单列的 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'))
  
# rename the name column with first_name
data1=rename(data1,first_name=name)
print(data1)
print("====================")
  
# rename the address column with city
rename(data1,city=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'))
  
# rename multiple columns
# name with first_name
# id with roll_no
# address with street
rename(data1,first_name=name,roll_no=id,street=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'))
  
# convert all columns to upper
rename_with(data1,toupper)
print("==============")
  
# convert all columns to lower
rename_with(data1,tolower)


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'))
  
# rename multiple columns
# name with first_name
# id with roll_no
# address with street
rename(data1,c(first_name=name,roll_no=id,street=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'))
  
# rename multiple columns
# name with first_name
# id with roll_no
# address with street
rename(data1,first_name=name,roll_no=id,street=address)

输出:

方法 2:使用 rename_with()

rename_with() 用于更改列的大小写。

  • 大写:要转换为大写,将数据帧的名称和 toupper 一起传递给函数,该函数告诉函数将大小写转换为大写。

句法:

  • 小写:要转换为小写,数据帧的名称和 tolower 将传递给函数,该函数告诉函数将大小写转换为小写。

句法:

示例:将列转换为上下列的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'))
  
# convert all columns to upper
rename_with(data1,toupper)
print("==============")
  
# convert all columns to lower
rename_with(data1,tolower)

输出

使用 rename()函数一次重命名多列

我们可以使用将列传递给向量的向量一次重命名多个列。

句法:

示例:一次重命名多个列的 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'))
  
# rename multiple columns
# name with first_name
# id with roll_no
# address with street
rename(data1,c(first_name=name,roll_no=id,street=address))

输出: