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] 1 2 3 4 5
x2 <- letters[1:5]                # Create second example vector
x2                                # Print second example vector
# [1] "a" "b" "c" "d" "e"
x3 <- c(4, 1, 7, 7, 5)            # Create third example vector
x3                                # Print third example vector
# [1] 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


table 1 data frame create data frame from vectors r


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.



Furthermore, you might read the related tutorials on Statistics Globe:


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.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

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.