which() function help page precision

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

which() function help page precision

buzon informatica, ige
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: which() function help page precision

Peter Dalgaard-2

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
 

> On 16 Nov 2018, at 11:08 , buzon informatica, ige <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: which() function help page precision

Martin Maechler
>>>>> 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
    >> <[hidden email]> 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]]
    >>
    >> ______________________________________________
    >> [hidden email] 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: [hidden email] Priv: [hidden email]

    > ______________________________________________
    > [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: which() function help page precision

Juan Gomez
In reply to this post by buzon informatica, ige
Hi again (, I am the PO from my own email account)
I agree that the word  "basically" puts the NA issues aside. But my
point is that  R subsetting  behavior when there are  NAs in a logical
index is quite tricky to say the less, and deserves the trouble of
pointing it out in every place it is appropiate. As  "which()" is the
function I use to overcome this issue, I thought it would be good to
emphasize that  it solves this situation in a different way that the
subsetting operation does.
I am aware that it is clearly stated  above in the help page though.
But I still would change the expression rather than "hiding" this distinction.
And the "diplomatic refinement seems even better to me.

______________________________________________
[hidden email] 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.