當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 rename()用法及代碼示例


rename()R 語言中的函數用於根據舊名稱重命名 DataFrame 的列名稱。

用法: rename(x, names)

參數:
x: DataFrame
names:舊名和新名

範例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



相關用法


注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Rename Columns of a Data Frame in R Programming – rename() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。