Example:
dataframe1 has following rows and columns.
+---------+---------+---------+---------+---------+
| Column1 | Column2 | Column3 | Column4 | Column5 |
+---------+---------+---------+---------+---------+
| A | B | C | D | E |
| P | Q | R | S | T |
| J | K | L | M | N |
+---------+---------+---------+---------+---------+
dataframe2 has following rows and columns.
+---------+---------+---------+---------+---------+
| Column1 | Column2 | Column3 | Column4 | Column5 |
+---------+---------+---------+---------+---------+
| B | D | E | M | N |
| Q | S | T | R | A |
| M | Q | R | A | T |
| A | B | C | D | E |
+---------+---------+---------+---------+---------+
What I want to do is first iterate through dataframe1 and check if the first row of dataframe1 is present in dataframe2 . Here in this example, first row of dataframe1 is present in forth row of dataframe2 . In the same way I want to check if all the rows of dataframe1 are present in dataframe2 . I could achieve this with multiple for loops. But, I would like to do it in a simpler and faster way.
We could paste
the rows in each dataset ( do.call(paste,...)
) and compare them using %in%. The output will be a logical vector. If
%in%. The output will be a logical vector. If
%in%. The output will be a logical vector. If
all the values from the
paste` output in 'df1' are present in 'df2', it will give 'TRUE' or else 'FALSE
all(do.call(paste, df1) %in% do.call(paste, df2))
Another option is using anti_join
from `dplyr'. If all the rows in 'df1' are found in 'df2', the output will be '0' row.
library(dplyr)
nrow(anti_join(df1, df2))==0
Base package. TRUE in case all are present, FALSE otherwise:
nrow(unique(merge(df1, df2))) == nrow(unique(df1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.