How to add Header to Dataframe in R ?

27 May, 2021
A header necessarily stores the names or headings for each of the columns. It basically helps the user to identify the role of the respective column in the data frame. The top row containing column names is called the header row of the data frame. In this article, we will learn how to add a Header to a Dataframe in the R programming language.
Dataframe in use:
colnames() function in R is used to set headers or names to columns of a dataframe or matrix.
Syntax: colnames(dataframe) <- c(“col_name-1”, “col_name-2”, “col_name-3”, “col_name-4”,.....)
Parameters:
dataframe object without header
In the code below, we have first created a sample dataframe without headers, then we have created a vector of headers or column names. Then we have used the function colnames(dataframe_name) to assign the headers to the dataframe.
Example:
R
df <- data.frame(c(11:14), c(5:8), letters[17:20], "df") print("Sample Dataframe with automatically assigned header")df   colnames(df) <- c("col-1","col-2","col-3","col-4")print("Dataframe with manually assigned Header")df
  
Output:
 
[1] "Sample Dataframe with automatically assigned header"
  c.11.14. c.5.8. letters.17.20. X.df.
1       11      5              q    df
2       12      6              r    df
3       13      7              s    df
4       14      8              t    df
[1] "Dataframe with manually assigned Header"
  col-1 col-2 col-3 col-4
1    11     5     q    df
2    12     6     r    df
3    13     7     s    df
4    14     8     t    df
names() function in R Language is used to get or set the name of an Object. This function takes the object i.e. vector, matrix, or data frame as an argument along with the value that is to be assigned as a name to the object.
 
Syntax: names(dataframe) <- c(“col_name-1”, “col_name-2”, “col_name-3”, “col_name-4”,.....)
Parameters:
dataframe object without header
In the code below, we have first created a sample dataframe without headers, then we have created a vector of headers or column names. Then we have used the function names(dataframe_name) to assign the headers to the dataframe. 
Example: 
R
df <- data.frame(c(11:14), c(5:8), letters[17:20], "df") print("Sample Dataframe with automatically assigned header")df   names(df) <- c("col-1","col-2","col-3","col-4")print("Dataframe with manually assigned Header")df
Output:
[1] "Sample Dataframe with automatically assigned header"
  c.11.14. c.5.8. letters.17.20. X.df.
1       11      5              q    df
2       12      6              r    df
3       13      7              s    df
4       14      8              t    df
[1] "Dataframe with manually assigned Header"
  col-1 col-2 col-3 col-4
1    11     5     q    df
2    12     6     r    df
3    13     7     s    df
4    14     8     t    df
sudhanshublaze
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 ?