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" |
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 |
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" |
class(my_data_frame) # Checking data type # "data.frame"