Hi,
Try this:
Not sure whether this is the fastest:
set.seed(932)
vec1<-sample(1:10,6,replace=TRUE)
 vec2<-sample(1:7,3,replace=TRUE)
 vec2[vec2%in%vec1]
#[1] 5
library(rbenchmark)
 benchmark(isTRUE(all(vec2%in%vec1)),replications=1e4)
#                         test replications elapsed relative user.self sys.self
#1 isTRUE(all(vec2 %in% vec1))        10000   0.295        1     0.272        0
#  user.child sys.child
#1          0         0

A.K.



----- Original Message -----
From: Atte Tenkanen <atte...@utu.fi>
To: "r-help@r-project.org" <r-help@r-project.org>
Cc: 
Sent: Thursday, September 27, 2012 5:00 PM
Subject: [R] How to test if there is a subvector in a longer vector

Hi,

There are certainly several ways to test, whether a longer vector includes a 
subvector. 
For instance, c(1,4,6) is included in c(2,1,1,4,6,3). How to test this and 
which would be the fastest way to do it?

Best,

Atte Tenkanen, FT, MuM
http://users.utu.fi/attenka/
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to