Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Remove rows from a data frame that exists in another data frame in R.
To remove rows from a data frame that exists in another data frame, we can use subsetting with single square brackets. This removal will help us to find the unique rows in the data frame based on the column of another data frame.
Check out the below Examples to understand how it can be done.
Example 1
Following snippet creates a sample data frame −
xThe following dataframe is created
x y 1 2 0 2 1 2 3 1 3 4 2 1 5 0 0 6 3 3 7 1 3 8 0 2 9 3 2 10 2 0 11 2 1 12 1 6 13 1 2 14 2 1 15 4 5 16 2 2 17 1 4 18 0 1 19 0 1 20 2 2Add the following code to the above snippet −
xThe following dataframe is created
a b 1 4 0 2 3 6 3 4 6 4 1 3 5 5 3 6 5 7 7 5 2 8 4 6 9 4 6 10 4 3 11 3 6 12 4 4 13 4 2 14 5 2 15 4 3 16 3 7 17 4 6 18 5 3 19 3 3 20 9 3To remove rows in df1 based on column x that do not exist in column a of df2 on the above created data frame, add the following code to the above snippet −
xOutput
If you execute all the above given snippets as a single program, it generates the following Output −
x y 1 2 0 4 2 1 5 0 0 8 0 2 10 2 0 11 2 1 14 2 1 16 2 2 18 0 1 19 0 1 20 2 2Example 2
Following snippet creates a sample data frame −
GrpThe following dataframe is created
Grp Rate 1 D 6 2 D 3 3 E 7 4 D 6 5 B 6 6 D 3 7 D 3 8 A 3 9 C 2 10 A 4 11 A 7 12 C 7 13 C 5 14 E 7 15 B 7 16 C 6 17 B 6 18 A 4 19 C 6 20 B 1Add the following code to the above snippet −
GrpThe following dataframe is created
Category Sales 1 E 12 2 C 11 3 D 9 4 E 13 5 G 5 6 C 9 7 D 14 8 D 11 9 D 8 10 F 11 11 F 17 12 G 15 13 F 12 14 D 9 15 G 13 16 C 9 17 C 12 18 F 7 19 E 7 20 C 8To remove rows in df_grp based on column Grp that do not exist in column Category of df_Sales on the above created data frame, add the following code to the above snippet −
GrpOutput
If you execute all the above given snippets as a single program, it generates the following Output −
Grp Rate 5 B 6 8 A 3 10 A 4 11 A 7 15 B 7 17 B 6 18 A 4 20 B 1
