Create Data Frame from Vectors in R (Example)
In this R tutorial you’ll learn how to construct a data frame from vectors.
Table of contents:
Sound good? Let’s start right away.
Creating Example Vectors
The first step is to define some example vectors:
x1 <- 1:5 # Create first example vector x1 # Print first example vector #  1 2 3 4 5
x2 <- letters[1:5] # Create second example vector x2 # Print second example vector #  "a" "b" "c" "d" "e"
x3 <- c(4, 1, 7, 7, 5) # Create third example vector x3 # Print third example vector #  4 1 7 7 5
As you can see based on the previous R codes, we have created three different vector objects called x1, x2, and x3.
Let’s combine these vectors in a single data set!
Example: Construct Data Frame from Vectors Using data.frame() Function
This example demonstrates how to convert multiple vectors to a data frame in the R programming language.
To accomplish this, we can apply the data.frame function as shown below. Within the data.frame function, we have to specify the names of the vector objects that we want to merge.
data <- data.frame(x1, x2, x3) # Create data frame data # Print data frame
Have a look at the previous table: It shows our three vectors unified in a data frame.
Video, Further Resources & Summary
Do you need more explanations on how to generate, define, and declare data sets based on vectors using the R programming codes of this page? Then you might have a look at the following video on my YouTube channel. I’m explaining the content of this article in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might read the related tutorials on Statistics Globe:
- How to Create a Data Frame in R
- Create List of Data Frames in R
- Combine Two Vectors into Data Frame or Matrix
- Convert List of Vectors to Data Frame
- Create Data Frame Row by Row in R
- R Programming Language
Summary: In this tutorial you have learned how to build and make a data frame from vectors in the R programming language. If you have additional questions, don’t hesitate to let me know in the comments below.
Statistics Globe Newsletter