Open In App

How to add a prefix to column names in R DataFrame ?

Last Updated : 28 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to add prefixes to column names in DataFrame in R Programming Language.

Dataset in use:

First  Second Third
1 a 7
2 ab 8
3 cv 9
4 dsd 10

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( prefix1, prefix2.., colnames(df), sep=)

Parameter :

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

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 prefix using the paste
# function in R
colnames(df) <- paste("Column" ,original_cols,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 : "
 Column-First Column-Second Column-Third
1            1             a            7
2            2            ab            8
3            3            cv            9
4            4           dsd           10

Multiple prefixes can also be prefixed before each column name, by specifying the strings before the column name in this method. 

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 prefix using the paste
# function in R
colnames(df) <- paste("Col" ,"No",original_cols,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 : "
 Col_No_First Col_No_Second Col_No_Third
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 prefixes 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( prefix1, prefix2, colnames(df))

Parameter :

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

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 prefix using the paste0 
# function in R
colnames(df) <- paste0("Col" ,original_cols)
  
# 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 : "
 ColFirst ColSecond ColThird
1        1         a        7
2        2        ab        8
3        3        cv        9
4        4       dsd       10


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads