Skip to content
Related Articles

Related Articles

Improve Article

How to add Header to Dataframe in R ?

  • Last Updated : 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:

c.11.14.c.5.8.letters.17.20.X.df.
115qdf
126rdf
137sdf
148tdf

Method 1: Using colnames() function

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

Method 2: Using names() function

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :