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

28 Apr, 2021
In this article, we will discuss how to add prefixes to column names in DataFrame in R Programming Language.
Dataset in use:
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 framedf <- data.frame(First = c(1,2,3,4),                 Second = c("a","ab","cv","dsd"),                 Third=c(7:10))  # print original data frameprint ("Original DataFrame : ")print (df)  # printing original colnames of# data frameoriginal_cols <- colnames(df)print ("Original column names ")print (original_cols)  # adding prefix using the paste# function in Rcolnames(df) <- paste("Column" ,original_cols,sep="-")  # print changed data frameprint ("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 framedf <- data.frame(First = c(1,2,3,4),                 Second = c("a","ab","cv","dsd"),                 Third=c(7:10))  # print original data frameprint ("Original DataFrame : ")print (df)  # printing original colnames # of data frameoriginal_cols <- colnames(df)print ("Original column names ")print (original_cols)  # adding prefix using the paste# function in Rcolnames(df) <- paste("Col" ,"No",original_cols,sep="_")  # print changed data frameprint ("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
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 framedf <- data.frame(First = c(1,2,3,4),                 Second = c("a","ab","cv","dsd"),                 Third=c(7:10))  # print original data frameprint ("Original DataFrame : ")print (df)  # printing original colnames # of data frameoriginal_cols <- colnames(df)print ("Original column names ")print (original_cols)  # adding prefix using the paste0 # function in Rcolnames(df) <- paste0("Col" ,original_cols)  # print changed data frameprint ("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
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
Merge DataFrames by Column Names in R
How to Sort a DataFrame in R ?