简体   繁体   中英

R: Examine to see if a Datatable is subset of another Datatable

How I can check to see if a data table is subset of another data table , regardless of the row and column order?
For instance, imagine someone rbinded the DT_x and DT_y with removing the duplicate and created DT_Z . Now, I want to know how I can compare DT_x and DT_Z and get the result which show/state that the DT_z is a subset of DT_Z ?

as very simple example:

 DT1 <- data.table(a= LETTERS[1:10], v=1:10)
 DT2 <- data.table(a= LETTERS[1:6], v=1:6)
 DT1
    a  v
 1: A  1
 2: B  2
 3: C  3
 4: D  4
 5: E  5
 6: F  6
 7: G  7
 8: H  8
 9: I  9
10: J 10

DT2
   a v
1: A 1
2: B 2
3: C 3
4: D 4
5: E 5
6: F 6

I am sure all.equal(DT1, DT2) will not answer my question.

I think you can use data.table 's fintersect() and fsetequal() :

is_df1_subset_of_df2 <- function(df1, df2) {
  intersection <- data.table::fintersect(df1, df2)
  data.table::fsetequal(df1, intersection)
}

The first line picks the elements in df1 that exists in df2 . The second line checks if that set is all of 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM