Skip to content
Prev 378071 / 398502 Next

Why does R do this?

Dear Nick,

The best solution is not to use which() but directy use the logical test.
This will work in case the condition is always FALSE and which() returns a
integer(0). And it is much faster too.
z <- y > 3
y[!z]

library(microbenchmark)
microbenchmark(
  y[!y > 3],
  y[-which(y > 3)]
)

Best regards,




ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry.onkelinx at inbo.be
Havenlaan 88 bus 73, 1000 Brussel
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
///////////////////////////////////////////////////////////////////////////////////////////

<https://www.inbo.be>


Op di 8 jan. 2019 om 10:29 schreef Nick Wray via R-help <
r-help at r-project.org>: