Skip to content
Back to formatted view

Raw Message

Message-ID: <CAAmySGMG3+h2ED9XZngJ9Go0EBhtJZcscHBA+z1X+tK9D7DZeQ@mail.gmail.com>
Date: 2011-11-16T15:07:48Z
From: R. Michael Weylandt
Subject: detect subset in series
In-Reply-To: <B40A715A-7079-4E9D-9839-BE6860DF2D1F@comcast.net>

If your data is also binary like that below, it might be useful/easier
to use regular expressions tools as well:

e.g.,
series <- c(1,1,1,0,1,0,0,1,1,0,0,0,0,1)
subset <- c(0,0,0,0)

print(regexpr(paste(subset, collapse = ""),paste(series, collapse = "")))

Michael

On Wed, Nov 16, 2011 at 9:55 AM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Nov 16, 2011, at 8:25 AM, threshold wrote:
>
>> Dear R Users, I am curious whether there is any simple solution o my
>> problem.
>>
>> My example 'series':
>> series <- c(1,1,1,0,1,0,0,1,1,0,0,0,0,1)
>>
>> My 'subset' of interest:
>> subset <- c(0,0,0,0)
>>
>> Is there any function which tells me that the subset exists in my series,
>> and gives me position where in series is starts. In provided example the
>> subset exists and covers the elements 10:13 of the 'series'.
>
> ?rle
> ?cumsum ? # may be needed to accumulate the number of postions before the
> rle$lengths criterion is first met.
>
> --
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.
>