R Bind Vectors with Different Length by Rows & Columns (2 Examples)

In this tutorial, I’ll explain how to join data objects with different length in the R programming language.

Example Data

x1 <- 11:13                    # Construct two example vectors in R
names(x1) <- letters[11:13]
x2 <- 11:15
names(x2) <- letters[11:15]

Example 1: Merge Data with Different Length by Rows

install.packages("dplyr")      # Install & load dplyr
library("dplyr")
bind_rows(x1, x2)              # Combine by rows
# # A tibble: 2 x 5
#       k     l     m     n     o
#   <int> <int> <int> <int> <int>
# 1    11    12    13    NA    NA
# 2    11    12    13    14    15

Example 2: Merge Data with Different Length by Columns

install.packages("qpcR")       # Install qpcR package
library("qpcR")                # Load qpcR
qpcR:::cbind.na(x1, x2)        # Combine by columns
#   x1 x2
# k 11 11
# l 12 12
# m 13 13
#   NA 14
#   NA 15

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