Combining Two Vectors in a Data Frame in R (Example Code)

This tutorial illustrates how to merge two vector objects in a data frame in the R programming language.

Example Data

x1 <- c(4, 1, 6, 7, 3)          # Creating vector No. 1
x1                              # Showing vector No. 1 in RStudio console
# 4 1 6 7 3
x2 <- c(5, 4, 5, 4, 5)          # Creating vector No. 2
x2                              # Showing vector No. 2 in RStudio console
# 5 4 5 4 5

Example: Apply data.frame Function to Merge 2 Vectors in a Data Frame

df <- data.frame(x1, x2)        # Creating data frame
df                              # Showing data frame in RStudio console
#   x1 x2
# 1  4  5
# 2  1  4
# 3  6  5
# 4  7  4
# 5  3  5

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.
You need to agree with the terms to proceed

Menu
Top