Skip to content

How to test if there is a subvector in a longer vector

7 messages · arun, David Winsemius, K. Elo +2 more

#
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/
#
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.
#
On Sep 27, 2012, at 2:00 PM, Atte Tenkanen wrote:

            
[1] TRUE
#
Sorry. I should have mentioned that the order of the components is important.

So c(1,4,6) is accepted as a subvector of c(2,1,1,4,6,3), but not of c(2,1,1,6,4,3).

How to test this?
________________________________________

Cc: R help
Aihe: Re: [R] How to test if there is a subvector in a longer vector

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.
#
Hi!
28.09.2012 08:41, Atte Tenkanen wrote:
How about this:

--- code ---

g1<- c(2,1,1,4,6,3)
g2<- c(2,1,1,6,4,3)
t1<- c(1,4,6)
t2<-c(9,8)

!is.na(sum(match(t1,g1)))
[1] TRUE
!is.na(sum(match(t1,g2)))
[1] TRUE
!is.na(sum(match(t2,g1)))
[1] FALSE

--- code ---

Kind regads,
Kimmo
#
On 28-09-2012, at 07:41, Atte Tenkanen <attenka at utu.fi> wrote:

            
See this discussion for a variety of solutions.

http://r.789695.n4.nabble.com/matching-a-sequence-in-a-vector-td4389523.html#a4393453

Berend
#
Thank you!
_______________
L?hett?j?: Berend Hasselman [bhh at xs4all.nl]
L?hetetty: 28. syyskuuta 2012 10:47
Vastaanottaja: Atte Tenkanen
Cc: R help
Aihe: Re: [R] How to test if there is a subvector in a longer vector
On 28-09-2012, at 07:41, Atte Tenkanen <attenka at utu.fi> wrote:

            
See this discussion for a variety of solutions.

http://r.789695.n4.nabble.com/matching-a-sequence-in-a-vector-td4389523.html#a4393453

Berend