From ripley at stats.ox.ac.uk  Wed Jun 18 18:49:51 2003
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Wed Jun 18 18:50:08 2003
Subject: [Rd] Private: Problem with tapply/lapply and sample (PR#3286)
In-Reply-To: <200306181638.h5IGcXJH019482@pubhealth.ku.dk>
Message-ID: <Pine.LNX.4.44.0306181742030.989-100000@gannet.stats>

You have already broadcast your address, and it is not reasonable to 
expect help anonymously.

In this case, the bug is in your reading of the help page for sample, 
quite possibly because you didn't read it.

Perhaps this might help your comprehension:

mysample <- function(x,...) {
    print(x)
    sample(x, ...)
}

> tapply(index,cluster,mysample)
[1] 1 2 3
[1] 4 5
[1] 6
$"1"
[1] 1 3 2

$"2"
[1] 5 4

$"3"
[1] 1 3 6 2 5 4

> sample(6)
[1] 5 2 3 4 1 6

Now look up what sample(6) does.

On Wed, 18 Jun 2003 peter.gedeck@pharma.novartis.com wrote:

> Full_Name: Peter Gedeck
> Version: R1.6.2 and R1.7.0
> OS: Windows XP
> Submission from: (NULL) (194.191.169.72)
> 
> 
> Hello,
> 
> I marked the bug report Private, as I don't want my email address on the web
> server. The problem that I found is best explained using an example.
> 
> index <- 1:6
> cluster <- c(1,1,1,2,2,3)
> tapply(index,cluster,sample)
> 
> gives 
> 
> $"1"
> [1] 2 1 3
> $"2"
> [1] 4 5
> $"3"
> [1] 3 2 4 1 6 5
> 
> The result for 1 and 2 is correct, the last line should however be 
> $"3"
> [1] 6
> and not return a sample containing all data. 

That is exactly what you asked for.  Working as documented is not a bug.

> Other functions seem to work
> correctly,
> 
> > tapply(index,cluster,min)
> 1 2 3 
> 1 4 6 
> > tapply(index,cluster,max)
> 1 2 3 
> 3 5 6 
> >  tapply(index,cluster,sum)
> 1 2 3 
> 6 9 6 
> 
> Another example which maybe gives an indication where the problem lies is 

The finger is still pointing at you!

-- 
Brian D. Ripley,                  ripley@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] Private: Problem with tapply/lapply and sample (PR#3286)
Prof Brian Ripley