简体   繁体   中英

Having two vectors is there a nicer way to test if one is included in another?

Given two vectors:

x = c('a','b')
lookup = c('a','c','d','e','f')

test if each element in x is present in lookup . One way of doing it:

all(!is.na(match(x, lookup)))

I find this solution a bit verbose for R and wonder if there is better/shorter version.

%in% does this:

all(x %in% lookup)
## [1] FALSE

Can also use setdiff . See the associated help page for other set operations.

setdiff(x,lookup)
[1] "b"
> as.logical(length( setdiff(x,lookup) ) )
[1] TRUE

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