Identify Shared Rows Between Two Data Frames in R (Example Code)
This article explains how to return all rows that exist in two data frames in the R programming language.
Example Data
df_A <- data.frame(col1 = 5, # Construct two data frames in R col2 = 12:17, col3 = letters[12:17], col4 = c("a", "a", "a", "b", "c", "d")) df_A # col1 col2 col3 col4 # 1 5 12 l a # 2 5 13 m a # 3 5 14 n a # 4 5 15 o b # 5 5 16 p c # 6 5 17 q d |
df_A <- data.frame(col1 = 5, # Construct two data frames in R col2 = 12:17, col3 = letters[12:17], col4 = c("a", "a", "a", "b", "c", "d")) df_A # col1 col2 col3 col4 # 1 5 12 l a # 2 5 13 m a # 3 5 14 n a # 4 5 15 o b # 5 5 16 p c # 6 5 17 q d
df_B <- data.frame(col1 = c(5, 5, 5, 5, 4, 3, 2, 1), col2 = 11:18, col3 = letters[11:18], col4 = "a") df_B # col1 col2 col3 col4 # 1 5 11 k a # 2 5 12 l a # 3 5 13 m a # 4 5 14 n a # 5 4 15 o a # 6 3 16 p a # 7 2 17 q a # 8 1 18 r a |
df_B <- data.frame(col1 = c(5, 5, 5, 5, 4, 3, 2, 1), col2 = 11:18, col3 = letters[11:18], col4 = "a") df_B # col1 col2 col3 col4 # 1 5 11 k a # 2 5 12 l a # 3 5 13 m a # 4 5 14 n a # 5 4 15 o a # 6 3 16 p a # 7 2 17 q a # 8 1 18 r a
Example: Return Common Rows of Two Data Frames in R
install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr |
install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr
df_shared <- inner_join(df_A, df_B) # Joining data frames df_shared # Displaying shared rows of data frames # col1 col2 col3 col4 # 1 5 12 l a # 2 5 13 m a # 3 5 14 n a |
df_shared <- inner_join(df_A, df_B) # Joining data frames df_shared # Displaying shared rows of data frames # col1 col2 col3 col4 # 1 5 12 l a # 2 5 13 m a # 3 5 14 n a
Related Tutorials & Further Resources
Have a look at the following R tutorials. They illustrate topics such as merging and variables: