How to Construct a Data Frame from Vectors in R (Example Code)
In this tutorial you’ll learn how to create a data frame from vectors in the R programming language.
Creation of Example Data
vec_A <- c(1, 3, 6, 8) # Constructing three vectors vec_B <- c("x", "y", "y", "x") vec_C <- c(7, 5, 9, 0) |
vec_A <- c(1, 3, 6, 8) # Constructing three vectors vec_B <- c("x", "y", "y", "x") vec_C <- c(7, 5, 9, 0)
Example: Create Data Frame Based On Vector Objects Using data.frame() Function
my_df <- data.frame(vec_A, # Combine vectors in data frame vec_B, vec_C) my_df # vec_A vec_B vec_C # 1 1 x 7 # 2 3 y 5 # 3 6 y 9 # 4 8 x 0 |
my_df <- data.frame(vec_A, # Combine vectors in data frame vec_B, vec_C) my_df # vec_A vec_B vec_C # 1 1 x 7 # 2 3 y 5 # 3 6 y 9 # 4 8 x 0
Related Articles & Further Resources
You may find some related R programming language tutorials on topics such as indices, graphics in R, and ggplot2 below: