Skip to content
Back to formatted view

Raw Message

Message-ID: <50653F6F.1090405@nic.fi>
Date: 2012-09-28T06:10:55Z
From: K. Elo
Subject: How to test if there is a subvector in a longer vector
In-Reply-To: <7953BA1D7779E746994B38A00362416C0B31E72B@exch-mbx-02.utu.fi>

Hi!

28.09.2012 08:41, Atte Tenkanen wrote:
> 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?

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