Re: [R] aggregate

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Wed 31 Aug 2005 - 00:57:04 EST

maybe you could use something like this:

dat <- data.frame(id = rep(1:3, 3), date = as.Date(rep(c("2005-08-25", "2005-08-26", "2005-08-29"), each = 3)))

########################

do.call("rbind", lapply(split(dat, dat$id), function(x) data.frame(id = x$id[1], start = min(x$date), end = max(x$date))))

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


> How can I aggregate this data.frame to list the min and max date for
> each unique id?
>
>>From this :
>> r = data.frame(id=rep(seq(1:3), 3), date=
>> as.Date(c(rep("2005-08-25",3), rep("2005-08-26",3),
>> rep("2005-08-29", 3)), "%Y-%m-%d"))
>> r
> id date
> 1 2005-08-25
> 2 2005-08-25
> 3 2005-08-25
> 1 2005-08-26
> 2 2005-08-26
> 3 2005-08-26
> 1 2005-08-29
> 2 2005-08-29
> 3 2005-08-29
>
> I want to get to this:
>>
> id start end
> 1 2005-08-25 2005-08-29
> 2 2005-08-25 2005-08-29
> 3 2005-08-25 2005-08-29
>
> I tried aggregate and aggregate.data.frame but the date column keeps
> getting converted into a number.
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Aug 31 01:00:47 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 16:08:49 EST