Ignore Variable Names when Using rbind() in R (Example Code)

In this tutorial, I’ll explain how to row-bind data frames by the column index in R.

Creation of Example Data

df_A <- data.frame(col1 = 10:15,              # Create two data frames in R
                   col2 = "x")
df_A
#   col1 col2
# 1   10    x
# 2   11    x
# 3   12    x
# 4   13    x
# 5   14    x
# 6   15    x
df_B <- data.frame(col3 = 100:105,
                   col4 = "y")
df_B
#   col3 col4
# 1  100    y
# 2  101    y
# 3  102    y
# 4  103    y
# 5  104    y
# 6  105    y

Example: Bind Data Frames by Column Index Instead of Name

df_B_rename <- setNames(df_B, names(df_A))    # Harmonize column names
rbind(df_A, df_B_rename)                      # Applying rbind function
#    col1 col2
# 1    10    x
# 2    11    x
# 3    12    x
# 4    13    x
# 5    14    x
# 6    15    x
# 7   100    y
# 8   101    y
# 9   102    y
# 10  103    y
# 11  104    y
# 12  105    y

Further Resources & Related Articles

Below, you can find some further resources on topics such as merging, extracting data, and indices.

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