I need to reshape from long into wide format. I do not have a times variable. What is the easiest way to create a time variable for each id in the following dataset for subsequent reshaping in base r (not the reshape package)?
h<-seq(from=as.Date("2005-06-01"), to=as.Date("2008-06-30"), by=1)
a<-data.frame(id=sample(c(1:100),300,replace=T),val=rnorm(n=300),date=sample(h,300,replace=T))
//M
Here is one possible approach. It uses
aveto create a “time” variable according to how many times an “id” occurs, which, to me, sounds like what you’re looking for.Your data, but ordered (and using
set.seedso others can reproduce it):You’ll end up with 8 “times”, checked by using
table.