I’ve looked all over the web including StackOverflow, and tested various things before asking this question, but pardon me if I missed an excellent answer.
I see lots of help for the reshape function (and the package too, but I can’t get either to do what I need). I have a “time” variable that differs by subject, e.g., it is not time1, time2, time3. I would like to make a wide data set that treats each unique time value by subject ID as just “time1”, “time2”, “time3”, but I need to save the dates. To make this concrete, here is some sample data:
Id<-c(1, 1,1, 2,2,2, 3)
date<-c("Jan10", "Jun11", "Dec11", "Feb10", "May10", "Dec10", "Jan11")
Score<-c(52, 43, 67, 56, 33, 21, 20)
format2<-data.frame(Id, date, Score)
format2
Id date Score
1 1 Jan10 52
2 1 Jun11 43
3 1 Dec11 67
4 2 Feb10 56
5 2 May10 33
6 2 Dec10 21
7 3 Jan11 20
I would like it to look like this:
Id date1 Score1 date2 Score2 date3 Score3
1 Jan10 52 Jun11 43 Dec11 67
2 Feb10 56 Dec10 21 May10 33
3 Jan11 20 NA NA NA NA
Thank you for any help and my apologies if I have missed an obvious answer.
You need to generate a
timevariable, which can be done quickly usingave():Some people prefer the
reshape2package because of its syntax, but even there, you need to have atimevariable before you can do anything interesting.Continuing from above (where the time variable was created):