Skip to content
Prev 140326 / 398506 Next

Compare two data sets

<amarkey at uiuc.edu> wrote in
news:20080325101909.BDK93111 at expms2.cites.uiuc.edu:
I do not understand what it would mean to remove elements so "they 
would look the same". Why wouldn't you just use the smaller set?
You might want to look at the %in% function. These examples created 
with neither dat1 nor dat2 being proper subsets of the other.

dat1 <- paste('a', 1:6, sep='')
dat2 <- paste('a', c(2,4:6,8,9,10), sep='')
[1] "a1" "a2" "a3" "a4" "a5" "a6"
[1] "a2"  "a4"  "a5"  "a6"  "a8"  "a9"  "a10"


dat2 %in% dat1
#[1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE

dat1 %in% dat2
#[1] FALSE  TRUE FALSE  TRUE  TRUE  TRUE

### And then use the logical vectors as index arguments
### to  first get the common elements
[1] "a2" "a4" "a5" "a6"
[1] "a2" "a4" "a5" "a6"

### And then to find the non-shared elements
[1] "a8"  "a9"  "a10"
[1] "a1" "a3"