How to Convert a Vector with Names as data.frame in R (Example Code)

In this tutorial, I’ll show how to convert a vector with names to the data.frame class in the R programming language.

Constructing Example Data

my_vector <- LETTERS[1:5]                          # Creating vector
names(my_vector) <- paste0("NAME_", 1:5)
my_vector                                          # Printing vector
# NAME_1 NAME_2 NAME_3 NAME_4 NAME_5 
#    "A"    "B"    "C"    "D"    "E"

Example: Change Named Vector to Data Frame Class

my_data_frame <- data.frame(as.list(my_vector))    # Converting vector to data.frame
my_data_frame                                      # Printing data.frame
#   NAME_1 NAME_2 NAME_3 NAME_4 NAME_5
# 1      A      B      C      D      E
class(my_data_frame)                               # Checking data type
# "data.frame"

Leave a Reply

Your email address will not be published.

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