# [R] Identifying one or more TRUE in the middle of an array

Peter Langfelder peter.langfelder at gmail.com
Sat Jun 7 00:10:42 CEST 2014

Not sure if this is better than your "brute force" and you may be able
to simplify it...

notStart = function(x)
{
n = length(x)
i0 = which(x);
n0 = length(i0);
i0!=c(1:n)[1:n0];
}

notStartNorEnd = function(x) { which(x)[notStart(x) & rev(notStart(rev(x)))] }

notStartNorEnd(c(F, F, F))
##integer(0)
notStartNorEnd(c(F, F, F, T))
##integer(0)
notStartNorEnd(c(T, F, F, F, T))
##integer(0)
notStartNorEnd(c(T, F, T, T, F, F, T))
[1] 3 4
notStartNorEnd(c(T, T, F, T, T, F, F, T, T, T))
[1] 4 5
notStartNorEnd(c(T, T, F, T, T, F, F))
[1] 4 5

Peter

On Fri, Jun 6, 2014 at 2:45 PM, Fisher Dennis <fisher at plessthan.com> wrote:
> R 3.1.0
> OS X
>
> Colleagues
>
> I have an array (I am using T/F rather than TRUE/FALSE for convenience) that could have patterns like:
>         c(T, T, T, F, F, F, T, F, T, T, T)              ## T at either end, a single T in the middle
>         c(F, F, F, F, F, T, F, F, T, T, T)              ## T at the tail end, a single T in the middle
>         c(T, T, T, F, F, T, T, F, F, F, F)              ## T at the front end, two T in the middle
>         c(T, T, T, F, F, T, T, F, T, F, F)              ## T at the front end, three T in the middle (not contiguous)
>         c(F, F, F, F, F, T, F, F, F, F, F)              ## no T at either end, a single T in the middle
> There might (or might not) be one or more T at the beginning (or the end).
> There might or might not be one or more T in the middle (not in a series that continues to either end) and the position of these T values varies.
>
> I am trying to identify the indices (if any) of these T values in the middle
> A brute force approach would be to strip off any contiguous T values from each end, then look for any remaining T values.  Can anyone propose a more clever approach?
>
> Dennis
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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