📜  如何为 R DataFrame 中的列名添加后缀?

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

如何为 R DataFrame 中的列名添加后缀?

数据框中的每一列都由一个名称定义,称为列名。它可能是数字或字符串值的类型。在本文中,我们将讨论如何在 R 编程语言中为 DataFrame 中的列名添加后缀。

方法一:使用 paste() 方法

为了修改列名,可以使用R中的paste函数。 paste() 方法可用于将字符串向量连接在一起以形成更大的字符串或句子。使用在 paste函数指定的分隔符连接字符串向量参数。更改必须保存到原始字符串,并且不会自行保留。字符串按照方法中的规范顺序连接在一起。如果我们在 R 列表对象上指定它,则该方法连续应用于每个字符串。

例子



R
# declare data frame 
df <- data.frame(c1=c(1,2,4), c2=c("Ab","Cd","Ef"),
                 c3=c(0.1,0.2,0.3))
  
print("Original DataFrame : ")
print(df)
  
print("Original col names")
print(colnames(df))
  
# adding suffix to column names 
colnames(df) <- paste(colnames(df),"new",sep="_")
  
print("New DataFrame : ")
print(df)
  
print("New col names")
print(colnames(df))


R
# declaring a data frame
df <- data.frame(First = c(1,2,3,4) ,
                 Second = c("a","ab","cv","dsd"),
                 Third=c(7:10))
  
# print original data frame
print ("Original DataFrame : ")
print (df)
  
# printing original colnames of 
# data frame
original_cols <- colnames(df)
print ("Original column names ")
print (original_cols)
  
# adding suffix using the paste
# function in R
colnames(df) <- paste(original_cols,"1","modified",sep="-")
  
# print changed data frame
print ("Modified DataFrame : ")
print (df)


R
# declaring a data frame
df <- data.frame(c1 = c(1,2,3,4) , 
                 c2 = c("a","ab","cv","dsd"))
  
# print original data frame
print ("Original DataFrame : ")
print (df)
  
# printing original colnames of 
# data frame
original_cols <- colnames(df)
print ("Original column names ")
print (original_cols)
  
# adding suffix using the paste0 
# function in R
colnames(df) <- paste0(original_cols,"changed")
  
# print changed data frame
print ("Modified DataFrame : ")
print (df)


输出

[1] "Original DataFrame : "
 c1 c2  c3
1  1 Ab 0.1
2  2 Cd 0.2
3  4 Ef 0.3
[1] "Original col names"
[1] "c1" "c2" "c3"
[1] "New DataFrame : "
 c1_new c2_new c3_new
1      1     Ab    0.1
2      2     Cd    0.2
3      4     Ef    0.3
[1] "New col names"
[1] "c1_new" "c2_new" "c3_new"

通过在方法定义中指定两个以上的参数字符串,还可以将多个后缀添加到列名中。

例子

电阻

# declaring a data frame
df <- data.frame(First = c(1,2,3,4) ,
                 Second = c("a","ab","cv","dsd"),
                 Third=c(7:10))
  
# print original data frame
print ("Original DataFrame : ")
print (df)
  
# printing original colnames of 
# data frame
original_cols <- colnames(df)
print ("Original column names ")
print (original_cols)
  
# adding suffix using the paste
# function in R
colnames(df) <- paste(original_cols,"1","modified",sep="-")
  
# print changed data frame
print ("Modified DataFrame : ")
print (df)

输出

[1] "Original DataFrame : "
 First Second Third
1     1      a     7
2     2     ab     8
3     3     cv     9
4     4    dsd    10
[1] "Original column names "
[1] "First"  "Second" "Third"
[1] "Modified DataFrame : "
 First-1-modified Second-1-modified Third-1-modified
1                1                 a                7
2                2                ab                8
3                3                cv                9
4                4               dsd               10

方法 2:使用 paste0() 方法

paste0 方法在功能上与 paste 方法完全相似,但它不提供在字符串参数之间自定义分隔符的功能。它会自动折叠字符串参数并将它们连接起来以生成一个没有任何空格的更大的字符串。也可以使用这种方法添加多个后缀。如果我们在 R 列表对象上指定它,则该方法连续应用于每个字符串。

例子:

电阻

# declaring a data frame
df <- data.frame(c1 = c(1,2,3,4) , 
                 c2 = c("a","ab","cv","dsd"))
  
# print original data frame
print ("Original DataFrame : ")
print (df)
  
# printing original colnames of 
# data frame
original_cols <- colnames(df)
print ("Original column names ")
print (original_cols)
  
# adding suffix using the paste0 
# function in R
colnames(df) <- paste0(original_cols,"changed")
  
# print changed data frame
print ("Modified DataFrame : ")
print (df)

输出

[1] "Original DataFrame : "
 c1  c2
1  1   a
2  2  ab
3  3  cv
4  4 dsd
[1] "Original column names "
[1] "c1" "c2"
[1] "Modified DataFrame : "
 c1changed c2changed
1         1         a
2         2        ab
3         3        cv
4         4       dsd