Convert Row to Header of Data Frame in R (Example Code)

This page explains how to use a row as column names in the R programming language.

Example Data

my_df <- data.frame(x = letters[1:3],    # Constructing example data
                    y = letters[2:4],
                    z = letters[3:5])
my_df                                    # Showing example data in RStudio console
#   x y z
# 1 a b c
# 2 b c d
# 3 c d e

Example: Set First Data Frame Row as Variable Names

colnames(my_df) <- my_df[ , 1]           # Using first row as colnames
my_df                                    # Showing new data frame in console
#   a b c
# 1 a b c
# 2 b c d
# 3 c d e

Related Tutorials & Further Resources

Here, you can find some additional resources on topics such as matrices, merging, and naming data.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top