How to add a prefix to column names in R DataFrame ?
Last Updated :
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:
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
Similar Reads
How to add suffix to column names in R DataFrame ? 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
4 min read
How to add column to dataframe in R ? In this article, we are going to see how to add columns to dataframe in R. First, let's create a sample dataframe. Adding Column to the DataFrame We can add a column to a data frame using $ symbol. syntax: dataframe_name $ column_name = c( value 1,value 2 . . . , value n)Â Here c() function is a vec
2 min read
How to change Row Names of DataFrame in R ? The rows are stacked together, each denoted by a unique name. By default, the integer identifiers beginning from 1 to the number of rows are assigned to the data frame by default. The task here is to change the Rows names in given dataframe using R programming. Dataset in use: First SecondThird1a72a
3 min read
How to assign column names based on existing row in R DataFrame ? In this article, we will discuss how assign column names or headers to a DataFrame based on rows in R Programming Language. Method 1 : Using as.character() method unlist() method in R is used to simulate the conversion of a list to vector. It simplifies to produce a vector by preserving all componen
3 min read
How to Add an Empty Column to DataFrame in R? In this article, we will discuss how to add an empty column to the dataframe in R Programming Language. Add One Empty Column to dataframe Here we are going to add an empty column to the dataframe by assigning column values as NA. Syntax: dataframe[ , 'column_name'] = NA where, dataframe  is the inpu
1 min read