How to assign column names based on existing row in R DataFrame ? - GeeksforGeeks

30 Apr, 2021
In this article, we will discuss how assign column names or headers to a DataFrame based on rows in R Programming Language.
unlist() method in R is used to simulate the conversion of a list to vector. It simplifies to produce a vector by preserving all components. Every element is the same as each of the values in that corresponding row. The as.character() method is then applied successively over this list, to deparse each of its elements individually. The as.character attempts to coerce its argument to the character type. The existing row is, therefore, assigned as a header of the data frame. The corresponding row can then be deleted or retained into the data frame. 
Syntax:
unlist(list)
Example:
R
# creating a data framedata_frame = data.frame(col1=c(1,2,3,-4),                        col2=c(8,9,5,10),                        col3=c(0,2,3,5))  # printing original data frameprint("Original Data Frame")print (data_frame)  # second row of the data frame is # assigned as the column namecolnames(data_frame) <- as.character(unlist(data_frame[2,]))  # second row is then deleted from # the data framedata_frame = data_frame[-2, ]  print("Modified Data Frame")print(data_frame)
Output
[1] "Original Data Frame"
 col1 col2 col3
1    1    8    0
2    2    9    2
3    3    5    3
4   -4   10    5
[1] "Modified Data Frame"
  2  9 2
1  1  8 0
3  3  5 3
4 -4 10 5
The lapply() function in R, returns a list of the same length as an input list object, each element of which is the result of the application of FUN to each of the corresponding elements of the list over which it is applied. 
Syntax: lapply(X, FUN, ...)
Parameter : 
X – The data frame or matrix
FUN – Any function in R can be specified as the second argument where in both in-built and user defined functions can be used.
If we use as.character as the FUN, then the variables of the data frame, all the variables of the data frame will be converted to the class “character”. The header or colnames can then be assigned to a particular row of the data frame. 
Example
R
# creating a data framedata_frame = data.frame(col1=c(1,-2,3,-4),                        col2=c(8,9,5,10),                        col3=c(0,2,3,5),                        col4=c(1.5,4.5,6.7,8.9))  # printing original data frameprint("Original Data Frame")print (data_frame)  # apply row4 as the headerdata_frame[] <- lapply(data_frame, as.character)colnames(data_frame) <- data_frame[4, ]  print("Modified Data Frame")print(data_frame)
Output
[1] "Original Data Frame"
 col1 col2 col3 col4
1    1    8    0  1.5
2   -2    9    2  4.5
3    3    5    3  6.7
4   -4   10    5  8.9
[1] "Modified Data Frame"
 -4 10 5 8.9
1  1  8 0 1.5
2 -2  9 2 4.5
3  3  5 3 6.7
4 -4 10 5 8.9
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 Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
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?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Convert Matrix to Dataframe in R