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. |
---|
11 | 5 | q | df |
12 | 6 | r | df |
13 | 7 | s | df |
14 | 8 | t | df |
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
How to add Header to Dataframe in R ?
Similar Reads
How to add header row to a Pandas Dataframe? A header necessarily stores the names or headings for each of the columns. It 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. There are two approaches to add header row to a Pandas Datafra
4 min read
How to Export DataFrame to CSV in R ? R Programming language allows us to read and write data into various files like CSV, Excel, XML, etc. In this article, we are going to discuss how to Export DataFrame to CSV file in R Programming Language. Approach:Â Write Data in column wise formatCreate DataFrame for these dataWrite Data to the CS
1 min read
How to Delete DataFrames in R? In R, a DataFrame is a data structure which can be two-dimensional, that is it can be used to hold data in rows and columns. To create a DataFrame, you can use the data.frame() function. but after you're done with a DataFrame, you may wish to remove it so that memory can be released or your workspac
3 min read
How to create dataframe in R Dataframes are fundamental data structures in R for storing and manipulating data in tabular form. They allow you to organize data into rows and columns, similar to a spreadsheet or a database table. Creating a data frame in the R Programming Language is a simple yet essential task for data analysis
3 min read
How to export dataframe to RDATA file in R? In this, article we are going to save the information of a data frame in an RDATA file and display the information of the file using R Programming language. To save the information of a data frame in a file and display the information of the file in R language is as follows: Using the save function
3 min read
How to merge dataframes in R ? In this article, we will discuss how to perform inner, outer, left, or right joins in a given dataframe in R Programming Language. Functions Used merge() function is used to merge or join two tables. With appropriate values provided to specific parameters, we can create the desired join. Syntax: mer
3 min read