[Rd] Inconsistent is.list results on 'by' objects

Ofek Shilon o|ek@h||on @end|ng |rom gm@||@com
Wed Nov 17 15:42:13 CET 2021


Thanks. 'by' is implemented by tapply, and it seems to behave rather
erratically for empty inputs:

> FUN = function(x) x[1,]
> FUNx <- function(x) FUN(df[x, , drop = FALSE])
> tapply(seq_len(10), df$b, FUNx) %>% storage.mode # array
> tapply(seq_len(0), c(), FUNx)   %>% storage.mode # logical
> tapply(seq_len(0), c(), NULL)   %>% storage.mode # integer

I think this warrants a fix, strictly in the tapply R code. WDYT?


On Wed, 17 Nov 2021 at 01:31, Kevin Ushey <kevinushey using gmail.com> wrote:

> You can see this a bit more clearly with e.g.
>
> > storage.mode(byy)
> [1] "list"
> > storage.mode(byy.empty)
> [1] "logical"
>
> So even though both objects have S3 class "by", they have a different
> underlying internal storage mode (as simplifying the result of 'by'
> has given you a 0-length logical, instead of a 0-length list).
>
> Best,
> Kevin
>
> On Tue, Nov 16, 2021 at 10:21 AM Bill Dunlap <williamwdunlap using gmail.com>
> wrote:
> >
> > Try adding simplify=FALSE to the call to by().
> >
> > -Bill
> >
> > On Tue, Nov 16, 2021 at 4:04 AM Ofek Shilon <ofekshilon using gmail.com>
> wrote:
> >
> > > Take this toy code:
> > > df       <- data.frame(a=seq(10), b=rep(1:2, 5))
> > > df.empty <- subset(df, a>10)
> > > byy       <- by(data=df,       INDICES=df$b, FUN=function(x) x[1,])
> > > byy.empty <- by(data=df.empty, INDICES=df.empty$b, FUN=function(x)
> x[1,])
> > >
> > > class(byy)       # "by"
> > > class(byy.empty) # "by"
> > >
> > > is.list(byy)        # TRUE
> > > is.list(byy.empty)  # FALSE!
> > >
> > >
> > > This behavior already messed up stuff elsewhere:
> > > https://github.com/Rdatatable/data.table/issues/5258
> > >
> > > I'd say any questions about the class of an object (whether 'class' or
> > > indirectly by 'is.**')
> > > should not have the answers depend on the specific contents of the
> object.
> > >
> > > Does this qualify as an R bug? Am I missing something?
> > >
> > >         [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-devel using r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-devel
> > >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-devel using r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



More information about the R-devel mailing list