Reputation: 14253
I have two vectors (sets) like this:
first<-c(1,2,3,4,5)
second<-c(2,4,5)
how can I detect that whether second
is subset of first
or not? is there any function for this?
Upvotes: 8
Views: 1066
Reputation: 87
If the order of the array elements matters, string conversion could help:
ord_match <- function(x,y){
m <- c(0,grep(paste0(x,collapse=""),
paste0(y,collapse=""), fixed = T))
return(as.logical(m)[length(m)])
}
Upvotes: -2
Reputation: 92300
Here's another
setequal(intersect(first, second), second)
## [1] TRUE
Or
all(is.element(second, first))
## [1] TRUE
Upvotes: 7