[R] which() function help page precision

peter dalgaard pd@lgd @ending from gm@il@com
Fri Nov 16 13:39:27 CET 2018


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 <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



More information about the R-help mailing list