# Why does R do this?

4 messages
Open this post in threaded view
|

## Why does R do this?

 y<-c(1,2,3) z<-which(y>3) z y<-y[-z] y In the work I'm doing I often have this situation and have to make sure that I condition on z being non-zero as y is now numeric(0) rather than the set c(1,2,3).  Why does R do this?  Wouldn't it be more sensible for R to simply leave the host set unchanged if there are no elements to take out? Any thoughts? Thanks, Nick Wray         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: 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 [hidden email] 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 /////////////////////////////////////////////////////////////////////////////////////////// Op di 8 jan. 2019 om 10:29 schreef Nick Wray via R-help < [hidden email]>: > y<-c(1,2,3) > z<-which(y>3) > z > y<-y[-z] > y > > In the work I'm doing I often have this situation and have to make sure > that I condition on z being non-zero as y is now numeric(0) rather than the > set c(1,2,3).  Why does R do this?  Wouldn't it be more sensible for R to > simply leave the host set unchanged if there are no elements to take out? > > Any thoughts? > > Thanks, Nick Wray >         [[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. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Why does R do this?

 In reply to this post by R help mailing list-2 Hi It is documented behaviour. "An empty index selects all values: this is most often used to replace all the entries but keep the attributes." so I presume that changing it could break huge amount of code. The only workaround could be to check "z" before using it for indexing. e.g. > if(length(z)==0) z <- length(y) + 1 > y[-z] [1] 1 2 3 > Cheers Petr > -----Original Message----- > From: R-help <[hidden email]> On Behalf Of Nick Wray via R- > help > Sent: Tuesday, January 8, 2019 10:29 AM > To: r-help <[hidden email]> > Subject: [R] Why does R do this? > > y<-c(1,2,3) > z<-which(y>3) > z > y<-y[-z] > y > > In the work I'm doing I often have this situation and have to make sure that I > condition on z being non-zero as y is now numeric(0) rather than the set > c(1,2,3).  Why does R do this?  Wouldn't it be more sensible for R to simply > leave the host set unchanged if there are no elements to take out? > > Any thoughts? > > Thanks, Nick Wray > [[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. Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Why does R do this?

 In reply to this post by R help mailing list-2 On 08/01/2019 4:28 a.m., Nick Wray via R-help wrote: > y<-c(1,2,3) > z<-which(y>3) At this point z is a vector with no entries in it. > z > y<-y[-z] -z is the same vector.  So y[z] and y[-z] are the same. > y > > In the work I'm doing I often have this situation and have to make sure that I condition on z being non-zero as y is now numeric(0) rather than the set c(1,2,3).  Why does R do this?  Wouldn't it be more sensible for R to simply leave the host set unchanged if there are no elements to take out? No, it wouldn't.  You asked for no entries, so you get no entries. Follow Thierry's advice, and don't use which() unless you really need a vector of indices, and are prepared for an empty one. Duncan Murdoch ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.