使用 Dplyr 重命名 R 中的列名
在本文中,我们将使用 R 编程语言中的 dplyr 包重命名列名。
使用中的数据集:
方法 1:使用 rename()
此方法用于重命名数据框中的列
句法:
rename(dataframe,new_columnname=old_column,………….,name,new_columnname=old_columnname)
其中 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 一起传递给函数,该函数告诉函数将大小写转换为大写。
句法:
rename_with(dataframe,toupper)
Where, dataframe is the input dataframe and toupper is a keyword that converts all columns to upper
- 小写:要转换为小写,数据帧的名称和 tolower 将传递给函数,该函数告诉函数将大小写转换为小写。
句法:
rename_with(dataframe,tolower)
where dataframe is the input dataframe and tolower is a keyword that converts all columns to lower.
示例:将列转换为上下列的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()函数一次重命名多列
我们可以使用将列传递给向量的向量一次重命名多个列。
句法:
rename(dataframe,c(newcolumn1=oldcolumn1,newcolumn2=oldcolumn2…,,newcolumn n=oldcolumn n))
示例:一次重命名多个列的 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))
输出: