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

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Sat Jun 7 00:27:48 CEST 2014

Here is my solution.

falses <- which(!x)
first.false <- head(falses, 1)
last.false <- tail(falses, 1)
which(x[first.false:last.false]) + first.false - 1

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx op inbo.be
To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey

Van: r-help-bounces op r-project.org [r-help-bounces op r-project.org] namens Fisher Dennis [fisher op plessthan.com]
Verzonden: vrijdag 6 juni 2014 23:45
Aan: r-help op stat.math.ethz.ch
Onderwerp: [R] Identifying one or more TRUE in the middle of an array

R 3.1.0


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 Fisher MD
P < (The "P Less Than" Company)
Phone: 1-866-PLessThan (1-866-753-7784)
Fax: 1-866-PLessThan (1-866-753-7784)

        [[alternative HTML version deleted]]

R-help op r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.

More information about the R-help mailing list