Open In App

How to add suffix to column names in R DataFrame ?

Improve
Improve
Like Article
Like
Save
Share
Report

Each of the columns in a data frame is defined by a name, known as the column name. It may be of the type of numerical or string value. In this article, we will discuss how to add a suffix to column names in DataFrame in R Programming Language. 

Method 1 : Using paste() method

In order to modify the column names, the paste function in R can be used. The paste() method, can be used for the concatenation of string vectors together to form a larger string or sentence. The string vector arguments are joined using the separator specified in the paste function. The changes have to be saved to the original string and are not retained on their own. The strings are concatenated in the order of specification in the method. The method is applied successively to each string in case we specify it over an R list object.

Syntax: paste( colnames(df), suffix1, suffix2 , sep=)

Parameter: 

  • colnames(df) – Column names of data frame
  • suffix – suffix string to be added to each column name
  • sep – separator to use between the strings

Example

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))


Output

[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"

Multiple suffixes can also be added to the column names, by specifying more than two argument strings in the method definition. 

Example

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)


Output

[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

Method 2 : Using paste0() method

paste0 method is exactly alike like the paste method in functionality, but it does not provide a feature of customized separator between the string arguments. It automatically collapses the string arguments and concatenates them to produce a larger string without any spaces. Multiple suffixes can also be added using this approach. The method is applied successively to each string in case we specify it over an R list object. 

Syntax: paste0( colnames(df), suffix1, suffix2 )

Parameter :

  • colnames(df) – Column names of data frame
  • suffix1.. – suffix string to be added to each column name. The strings are concatenated using sep=””.

Example:

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)


Output

[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


Last Updated : 28 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads