R How to Specify ID-Variables for Joining Data in dplyr (Example Code)

This article explains how to set up the column names in a merge with the dplyr package in the R programming language.

Preparing the Example

my_df1 <- data.frame(First_ID = 1:4,                        # First example data
                     x = 1)
my_df2 <- data.frame(Second_ID = 2:6,                       # Second example data
                     y = 2)
install.packages("dplyr")                                     # Install & load dplyr
library("dplyr")

Example: Specify Names of Joined Columns Using

my_df_join <- inner_join(my_df1, my_df2,                    # Joining data
                         by = c("First_ID" = "Second_ID"))  # Specifying ID names
my_df_join                                                  # Return merged data
#   First_ID x y
# 1        2 1 2
# 2        3 1 2
# 3        4 1 2

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