Combine Two Vectors into Data Frame or Matrix in R (2 Examples)

 

In this tutorial, I’ll illustrate how to merge two vector objects in a data frame or matrix in R.

Table of contents:

Let’s just jump right in!

 

Creation of Example Data

Have a look at the following example data:

vec1 <- 1:10                         # Create first vector
vec1                                 # Print first vector
#  [1]  1  2  3  4  5  6  7  8  9 10

The previous output of the RStudio console illustrates the structure of our first vector (or array). It consists of ten numeric values ranging from 1 to 10.

Let’s create a second vector object:

vec2 <- letters[1:10]                # Create second vector
vec2                                 # Print second vector
#  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

The second vector consists of ten character elements.

Note that both vectors have the same length (i.e. 10). This is important if we want to merge our two vectors in a data frame or matrix later on.

 

Example 1: Join Two Vectors into Data Frame Using data.frame Function

This example shows how to create a data frame based on our two vectors using the data.frame function:

my_data <- data.frame(vec1, vec2)    # Apply data.frame function
my_data                              # Print data frame
#    vec1 vec2
# 1     1    a
# 2     2    b
# 3     3    c
# 4     4    d
# 5     5    e
# 6     6    f
# 7     7    g
# 8     8    h
# 9     9    i
# 10   10    j

Have a look at the previous output of the RStudio console: It shows our new data frame!

 

Example 2: Join Two Vectors into Matrix Using cbind Function

The following R code explains how to construct a matrix containing our two vectors. For this, we can use the cbind function:

my_matrix <- cbind(vec1, vec2)       # Apply cbind function
my_matrix                            # Print matrix
#      vec1 vec2
#  [1,] "1"  "a" 
#  [2,] "2"  "b" 
#  [3,] "3"  "c" 
#  [4,] "4"  "d" 
#  [5,] "5"  "e" 
#  [6,] "6"  "f" 
#  [7,] "7"  "g" 
#  [8,] "8"  "h" 
#  [9,] "9"  "i" 
# [10,] "10" "j"

Note that all variables of matrices need to have the same data class. For that reason our numeric vector was automatically converted to the character class.

 

Video, Further Resources & Summary

Do you need more info on the topics of this tutorial? Then you may want to have a look at the following video of my YouTube channel. In the video, I show the contents of this article in a live programming session.

 

 

Furthermore, you could have a look at some of the other articles of this website. A selection of articles can be found below.

 

In this article you learned how to concatenate multiple vectors as columns in a data frame in the R programming language. In case you have further questions, please let me know in the comments section. Furthermore, don’t forget to subscribe to my email newsletter in order to receive updates on the newest articles.

 

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.

Top