[R] Odd behaviour of removing 'nothing' from an array or data frame
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Oct 31 15:19:09 CET 2006
But what if you wanted to get the subset of those rows containing
heights over 250 -- you would want zero rows to be returned:
On 10/31/06, Richard.Cotton at hsl.gov.uk <Richard.Cotton at hsl.gov.uk> wrote:
> I've just found some behaviour which strikes me as odd, but I'm not sure
> whether it's a bug or a feature. If you don't mind, I'd like to explain
> via a couple of examples.
>
> Let x = 1:10.
> Then intuitively, to me at least, the command x[-integer(0)] should leave
> x untouched. However the actual output under R2.4.0 is integer(0).
>
> A slightly more involved example demonstrates why I think this behaviour
> is back to front.
> First we define a data frame, in this case some people, with their
> heights.
> peoples.heights = data.frame(names = c("Alice", "Bob", "Carol"), heights =
> c(1.67, 1.85, 175))
>
> To make sure the heights are sensible, we define a filter out impossibly
> tall people.
> dubious.records = which(peoples.heights$heights > 2.5) #3
> peoples.heights = peoples.heights[-dubious.records,]
>
> This all works fine since dubious.records is not empty. However, if all
> the records had been entered properly, then we would get
> #dubious.records = integer(0)
>
> Then the command peoples.heights = peoples.heights[-dubious.records,]
> strips all the rows to give
> #[1] names heights
> #<0 rows> (or 0-length row.names)
>
> i.e. instead of removing the bad records, I've lost everything.
> I know that it's possible to recode this so problems don't occur, but the
> point is that the answer is unexpected.
>
> Can anybody explain if this behaviour is intentional or useful in some
> way, or is it an oversight?
>
> Regards,
> Richie.
>
> Mathematical Sciences Unit
> HSL
> Buxton
> SK17 9JN
> 01298 21(x8672)
>
>
> ------------------------------------------------------------------------
> ATTENTION:
>
> This message contains privileged and confidential informatio...{{dropped}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> 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.
>
More information about the R-help
mailing list