Message-ID: <971536df0903020506o17a16798laeaffa25a19d50a5@mail.gmail.com>
Date: 2009-03-02T13:06:15Z
From: Gabor Grothendieck
Subject: ave and grouping
In-Reply-To: <971536df0903020501i57e0a0ear2b89f3d73fd9e934@mail.gmail.com>
There was an error in the line below. It should have been the following (i.e.
the ix in extra[ix] was missing):
sleep$newcol <-
with(sleep, ave(1:nrow(sleep), group, FUN = function(ix)
extra[ix][b[ix][1]]))
On Mon, Mar 2, 2009 at 8:01 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Try performing ave over the indexes rather than over extra itself:
>
> sleep$newcol <-
> ?with(sleep, ave(1:nrow(sleep), group, FUN = function(ix) extra[b[ix][1]]))
>
> On Mon, Mar 2, 2009 at 4:28 AM, Patrick Hausmann
> <Patrick.Hausmann at uni-bremen.de> wrote:
>> Dear list,
>>
>> # I have a DF like this:
>> sleep$b ? <- c(rep(8,10), rep(9,10))
>> sleep$me ?<- with(sleep, ave(extra, group, FUN = mean))
>> sleep
>>
>> # I would like to create a new variable
>> # holding the b-th value of group 1 and 2.
>>
>> # This is not what I want, it takes always the '8' from group '1'
>> # and not the '9'
>> sleep$gr ?<- with(sleep, ave(extra, group, FUN = function(x) x[ b[1] ]))
>> sleep
>>
>> Thanks for any help!
>> Patrick
>>
>> ______________________________________________
>> 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.
>>
>