Fisher Dennis
2014-Jun-06 21:45 UTC
[R] Identifying one or more TRUE in the middle of an array
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]]
Peter Langfelder
2014-Jun-06 22:10 UTC
[R] Identifying one or more TRUE in the middle of an array
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.
ONKELINX, Thierry
2014-Jun-06 22:27 UTC
[R] Identifying one or more TRUE in the middle of an array
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
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx at inbo.be
www.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 at r-project.org [r-help-bounces at r-project.org] namens
Fisher Dennis [fisher at plessthan.com]
Verzonden: vrijdag 6 juni 2014 23:45
Aan: r-help at stat.math.ethz.ch
Onderwerp: [R] Identifying one or more TRUE in the middle of an array
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.
* * * * * * * * * * * * * 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.