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

K. Elo maillists at nic.fi
Fri Sep 28 08:10:55 CEST 2012


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




More information about the R-help mailing list