From ripley at stats.ox.ac.uk  Mon Jan 28 20:45:25 2008
From: ripley at stats.ox.ac.uk (ripley at stats.ox.ac.uk)
Date: Mon, 28 Jan 2008 20:45:25 +0100 (CET)
Subject: [Rd] tapply on empty data.frames (PR#10644)
Message-ID: <20080128194525.79666283462A@mail.pubhealth.ku.dk>

It's not about data frames: you did not pass tapply a data frame.

There's quite a few strange things here.  More likely you intended

> sapply(split(z1$a, z1$b), length)

but that gives list() whereas

> z2 <- subset(z,a == 4)
> sapply(split(z2$a, z2$b), length)
a b c d
0 0 0 1

is as one might expect.  This happens because split(z1$a, z1$b) is NULL, 
not a possible value according to the help page.

Back to tapply: it computes double rather than integer indices, but what 
it does in the end is

ansmat[numeric(0)] <- NULL

for a logical array ansmat.  That fails athough

i) there is nothing to replace
ii) replacing NULL by list() works.

Lots of other places happily promote NULL to list().

So there's something to fix in split() and in either [<- or lapply (or 
both).

The way the code is written it is not easy to make
x[<zero-length>] <- <anything> a no-op, but it think it probably should 
be.


On Sun, 27 Jan 2008, hilmar.berger at imise.uni-leipzig.de wrote:

> Full_Name: Hilmar Berger
> Version: 2.4.1/2.6.2alpha
> OS: WinXP
> Submission from: (NULL) (84.185.128.110)
>
>
> Hi all,
>
> If I use tapply on an empty data.frame I get an error. I'm not quite sure if one
> can actually expect the function to return with a result. However, the error
> message suggests that this case does not get handled well.
>
> This happens both in R-2.4.1 and 2.6.2alpha (version 2008-01-26).
>
>> z = data.frame(a = c(1,2,3,4),b=c("a","b","c","d"))
>> z1 = subset(z,a == 5)
>> tapply(z1$a,z1$b,length)
> Error in ansmat[index] <- ans :
>  incompatible types (from NULL to logical) in subassignment type fix
>
> Deleting unused factor levels from the group parameter gives:
>
>> tapply(z1$a,factor(z1$b),length)
> logical(0)
>
>
> Regards,
> Hilmar
>
> platform       i386-pc-mingw32
> arch           i386
> os             mingw32
> system         i386, mingw32
> status         alpha
> major          2
> minor          6.2
> year           2008
> month          01
> day            26
> svn rev        44181
> language       R
> version.string R version 2.6.2 alpha (2008-01-26 r44181)
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] tapply on empty data.frames (PR#10644)
ripley at stats.ox.ac.uk