在 R 编程中重命名数据框的列 – rename()函数
R 语言中的rename()
函数用于根据旧名称重命名数据框的列名。
Syntax: rename(x, names)
Parameters:
x: Data frame
names: Old name and new name
示例 1:
# R program to rename a Data Frame
# Adding Package
df <- library(plyr)
# Creating a Data Frame
df<-data.frame(row1 = 0:2, row2 = 3:5, row3 = 6:8)
print("Original Data Frame")
print(df)
print("Modified Data Frame")
# Renaming Data Frame
rename(df, c("row1"="one", "row2"="two", "row3"="three"))
输出:
[1] "Original Data Frame"
row1 row2 row3
1 0 3 6
2 1 4 7
3 2 5 8
[1] "Modified Data Frame"
one two three
1 0 3 6
2 1 4 7
3 2 5 8
示例 2:
# R program to modify names of a data frame
# Loading library
df <- library(plyr)
# Creating a data frame
df = data.frame(
"col1" = c("abc", "def", "ghi"),
"col2" = c("R", "Python", "Java"),
"col3" = c(22, 25, 45)
)
df
# Calling rename() function
rename(df, c("col1" = "Name", "col2" = "Language", "col3" = "Age"))
输出:
col1 col2 col3
1 abc R 22
2 def Python 25
3 ghi Java 45
Name Language Age
1 abc R 22
2 def Python 25
3 ghi Java 45