[R] which() function help page precision

Martin Maechler m@echler @ending from @t@t@m@th@ethz@ch
Fri Nov 16 18:52:33 CET 2018


>>>>> peter dalgaard 
>>>>>     on Fri, 16 Nov 2018 13:39:27 +0100 writes:

    > Well, "Basically, " is an excuse for not being
    > accurate. Making the code more complex doesn't really help
    > the explanation. It could be better to just add "(except
    > for NA handling)" or so.
    > -pd

or --- applying Swiss diplomacy finding a way-between ;-) --- 

" Basically, the result is (1:length(x))[x], or to cover more
  cases, including when x has NA's,
  seq_along(x)[!is.na(x) & x] "

to also cover the 0-length case and come pretty close to the "truth".

Martin


 
    >> On 16 Nov 2018, at 11:08 , buzon informatica, ige
    >> <ige.informatica using ige.eu> wrote:
    >> 
    >> The which() function help page states that, in the
    >> default case, what the function returns is: " Basically,
    >> the result is (1:length(x))[x]."  That would only be true
    >> if there are not any NA values in x. I think it would be
    >> more accurate to say: "Basically, the result is
    >> (1:length(x))[!is.na(x) & x]."
    >> 
    >> The "strange" (IMHO) behavior of logical indexing in R
    >> makes it necessary to exclude NA values.  For this
    >> reason, I use to wrap logical indices with which(). I
    >> would have written the above expression as:
    >> (1:length(x))[which(x)] But that would have been a really
    >> bad explanation of how the which() function works ;)
    >> 
    >> [[alternative HTML version deleted]]
    >> 
    >> ______________________________________________
    >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and
    >> more, see 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.

    > -- 
    > Peter Dalgaard, Professor, Center for Statistics,
    > Copenhagen Business School Solbjerg Plads 3, 2000
    > Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23
    > Email: pd.mes using cbs.dk Priv: PDalgd using gmail.com

    > ______________________________________________
    > R-help using r-project.org mailing list -- To UNSUBSCRIBE and
    > more, see 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