Re: [R] count the cumulative for each subject

From: Bart Joosen <Bartjoosen_at_hotmail.com>
Date: Mon, 24 Nov 2008 20:01:18 +0100


Peter,

After I made a small modification it worked: with(dat, ave(x1, subject, FUN=cumsum))

But what's the use of with?
If I use ave(x1, subject, FUN=cumsum), I get the same result?

Bart

> Peter,
>
> I actually took a look at the ave function, but couldn't manage to get it
> right.
> But when I try your code, I get "Error in as.vector(x, mode) : invalid
> argument 'mode'".
> Any ideas?
>
> Bart
>
> ----- Original Message -----
> From: "Peter Dalgaard" <P.Dalgaard_at_biostat.ku.dk>
> To: "bartjoosen" <bartjoosen_at_hotmail.com>
> Cc: <r-help_at_r-project.org>
> Sent: Monday, November 24, 2008 3:42 PM
> Subject: Re: [R] count the cumulative for each subject
>
>
> bartjoosen wrote:
>> How about:
>>
>> tapply(dat$x1,dat$subject,function(x) cumsum(x))
>> which gives you a list for each subject.
>>
>> this can be converted to a vector:
>> do.call("c",tapply(dat$x1,dat$subject,function(x) cumsum(x)))
>>
>> So if your data frame is ordered for your subjects:
>>
>> cbind(your.data.frame,do.call("c",tapply(dat$x1,dat$subject,function(x)
>> cumsum(x))))
>>
>>
>
> Sounds like a job for the eternally overlookked ave() function:
>
> with(dat, ave(x1, subject, cumsum))
>
>
> --
> O__ ---- Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B
> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
> (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk) FAX: (+45) 35327907
>
>



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. Received on Tue 25 Nov 2008 - 04:31:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 25 Nov 2008 - 07:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive