[R] How to test if there is a subvector in a longer vector

arun smartpink111 at yahoo.com
Fri Sep 28 05:13:23 CEST 2012


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 <attenka at utu.fi>
To: "r-help at r-project.org" <r-help at 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 at 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.





More information about the R-help mailing list