Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reshape in R when "time" values differ between subjects

Tags:

r

reshape

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.

like image 837
eastcoastR Avatar asked Dec 26 '22 13:12

eastcoastR


1 Answers

You need to generate a time variable, which can be done quickly using ave():

format2$time <- ave(format2$Id, format2$Id, FUN=seq_along)
reshape(format2, direction = "wide", idvar="Id", timevar="time")
#   Id date.1 Score.1 date.2 Score.2 date.3 Score.3
# 1  1  Jan10      52  Jun11      43  Dec11      67
# 4  2  Feb10      56  May10      33  Dec10      21
# 7  3  Jan11      20   <NA>      NA   <NA>      NA

Some people prefer the reshape2 package because of its syntax, but even there, you need to have a time variable before you can do anything interesting.

Continuing from above (where the time variable was created):

library(reshape2)
format2m <- melt(format2, id.vars=c("Id", "time"))
dcast(format2m, Id ~ variable + time)
#   Id date_1 date_2 date_3 Score_1 Score_2 Score_3
# 1  1  Jan10  Jun11  Dec11      52      43      67
# 2  2  Feb10  May10  Dec10      56      33      21
# 3  3  Jan11   <NA>   <NA>      20    <NA>    <NA>
like image 133
A5C1D2H2I1M1N2O1R2T1 Avatar answered Dec 29 '22 01:12

A5C1D2H2I1M1N2O1R2T1