[R] omitting integer(0) rows from data frame

William Dunlap wdunlap at tibco.com
Wed Oct 30 20:19:43 CET 2013


> > It would be less surprising if there were no rows omitted in the (B) case.

-integer(0) is identical to integer(0), so how could '[' handle them differently?

> Yes. It is surprising. It is also teh reason why the construction is also not returning what a
> user might expect:
> 
> dat[-which(dat$id >5), ]
> #[1] id  let
> #<0 rows> (or 0-length row.names)
> 
> (And yes I know that many people never use which with a logical. I'm just not one of
> those for what I consider good reasons.)
> 
> `subset` may be preferred,  at least for console interaction:
> 
>  subset( dat, !(id >5) )

Preferring to use which(logical) is ok, as long as you are careful, but subset() will not let you use which().
  > subset(dat, -which(id>5))
  Error in subset.data.frame(dat, -which(id > 5)) :
    'subset' must evaluate to logical

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of David Winsemius
> Sent: Wednesday, October 30, 2013 10:28 AM
> To: Jack Tanner
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] omitting integer(0) rows from data frame
> 
> 
> On Oct 30, 2013, at 6:04 AM, Jack Tanner wrote:
> 
> > I'm not sure if this is correct behavior or not, but it seems counterintuitive
> > to me:
> >
> > dat <- data.frame(id=1:5, let=letters[1:5])
> > # A. omits the first row
> > dat[- 1, ]
> >
> > # B. unexpectedly omits ALL rows
> > dat[- integer(0), ]
> >
> > It would be less surprising if there were no rows omitted in the (B) case.
> 
> Yes. It is surprising. It is also teh reason why the construction is also not returning what a
> user might expect:
> 
> dat[-which(dat$id >5), ]
> #[1] id  let
> #<0 rows> (or 0-length row.names)
> 
> (And yes I know that many people never use which with a logical. I'm just not one of
> those for what I consider good reasons.)
> 
> `subset` may be preferred,  at least for console interaction:
> 
>  subset( dat, !(id >5) )
>   id let
> 1  1   a
> 2  2   b
> 3  3   c
> 4  4   d
> 5  5   e
> 
> --
> 
> David Winsemius
> Alameda, CA, USA
> 
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list