Skip to content
Prev 264657 / 398502 Next

Reshape from long to wide format with date variable

Hi Pete,

Try the reshape function (see ?reshape for documentation).  It can be
a bit confusing, but its worth learning if you deal with multiple
observations per unit much.  Code inline does what you want (though
you might need a bit of tweaking to get pretty names, etc.

HTH,

Josh
On Wed, Jul 6, 2011 at 6:40 AM, Pete Pete <noxyport at gmail.com> wrote:
## create time variable
data1$time <- with(data1, ave(1:nrow(data1), id, FUN = seq_along))

wdata1 <- reshape(data1, idvar = "id", timevar = "time", direction = "wide")
id     date.1 ref.1     date.2 ref.2     date.3 ref.3
1 034 1997-09-28     2 1997-10-06     2       <NA>  <NA>
3 016 1997-11-04     1 2000-09-27     1 2003-07-20     2
6 340 1997-11-08     1 1997-11-08     1       <NA>  <NA>