Bryan Hanson
2010-Mar-14 02:39 UTC
[R] Why doesn't vec[-real.number] give an error or warning? Kids do the darndest things!
Hi all... My students were conflating grepping for a value in a vector to get the index, and then removing it with [-index], for instance like this: set.seed(17) v <- rnorm(20) s <- v[-1.18] They were trying to remove the 12th value in v, which is -1.18 or so. But the result is, as documented in ?Extract, to coerce 1.18 to the next lowest integer, and remove the 1st value of v, not the 12th value. This strikes me as something that ought to give an error or warning, or at least be in the R Inferno, but I couldn't find it there. Under what circumstances would the documented behavior be valuable? I can see that it avoids doing floor or as.integer in computations where an index is needed but real numbers are generated for whatever reason, but is that common? Thanks for any insight. Bryan ************* Bryan Hanson Acting Chair Professor of Chemistry & Biochemistry DePauw University, Greencastle IN USA
jim holtman
2010-Mar-14 04:39 UTC
[R] Why doesn't vec[-real.number] give an error or warning? Kids do the darndest things!
That notation 'v[-1.18]' is not going to remove the 12th entry if it was 1.18. They should have used: v[-which(v == 1.18)] to remove it, but it probably would not have worked and we would have refered them to FAQ 7.31. R is doing exactly what you are asking it to do. There are cases where I have used a real number and would not have expected an error. As you say, kids do the darnest things. I use the comment that "users always find new and unusual ways to use a system that the developers never envisioned". All we can do it tell them how it does work (for better or for worse) and even if they think it is wrong, we can not change it as this point in time to adapt to their way that they think it should work. R is a great learning experience for people and it allows you to experiment without breaking things. I still have to write little test cases every so often to understand how things work, especially if you have NAs in your data and you are trying to do some equality test or do a calculation on it. On Sat, Mar 13, 2010 at 9:39 PM, Bryan Hanson <hanson@depauw.edu> wrote:> Hi all... > > My students were conflating grepping for a value in a vector to get the > index, and then removing it with [-index], for instance like this: > > set.seed(17) > v <- rnorm(20) > s <- v[-1.18] > > They were trying to remove the 12th value in v, which is -1.18 or so. > > But the result is, as documented in ?Extract, to coerce 1.18 to the next > lowest integer, and remove the 1st value of v, not the 12th value. > > This strikes me as something that ought to give an error or warning, or at > least be in the R Inferno, but I couldn't find it there. > > Under what circumstances would the documented behavior be valuable? I can > see that it avoids doing floor or as.integer in computations where an index > is needed but real numbers are generated for whatever reason, but is that > common? > > Thanks for any insight. Bryan > ************* > Bryan Hanson > Acting Chair > Professor of Chemistry & Biochemistry > DePauw University, Greencastle IN USA > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
S Ellison
2010-Mar-15 02:31 UTC
[R] Why doesn't vec[-real.number] give an error or warning? Kids do the darndest things!
>>> Bryan Hanson <hanson at depauw.edu> 03/14/10 2:39 AM >>> >Under what circumstances would the documented behavior be valuable?<Inserts tongue in cheek:> It would be useful in all the circumstances in which it would be sensible to use a real number as a vector index. Not that I can think of any. Somebody else might, though. And it is possible that someone might calculate intended integer values from reals, leading to a real vector of indices. Under those circs, returning an error or warning wouldn't be helpful. So in this case R is kind of treating users like grown-ups who are assumed to know what they're doing. It's also pedagogically (is that a word?) useful. Users who confuse reals with integers may remember the lesson better if it fouls up their entire script with a problem that takes hours to track down than if it generates a polite warning they don't understand but accommodate by changing the script. And that kind of difference is occasionally terribly important in statistics and in general computing. So maybe it is useful to be allowed to foul up occasionally. <removes tongue from cheek> Steve E ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}