Can anyone help arranging long data to wide data, but complicated by linked results, ie listed in wide format identified by Study Number this repetitive results listed in wide format after the SN (I've shown an abbreviated table there are more results per patient listed along the bottom with repetitive in columns LabTest, LabDate, Result, Lower, Upper)...I've tried melt and recast, and binding columns but can't seem to get it to work. Over 1000 results to reformat so can't input results manually need to reformat a long data excel document in wide format in R Thank you
SN     LabTest     LabDate    Result Lower Upper
TD62   Creat       05/12/2004  22     30    90
TD62   AST         06/12/2004  652    6     45
TD58   Creat       26/05/2007  72     30    90
TD58   Albumin     26/05/2005  22     25    35  
TD14   AST         28/02/2007  234    6     45
TD14   Albumin     26/02/2007  15     25    35
Formatted data should look like this
SN LabTCode LabDate Result Lower Upper LabCode LabDate Result Lower Upper
TD62 Creat   05/12/04  22    30   90   AST     06/12/04  652   6    45
TD58 Creat   26/05/05  72    30   90   Alb     26/05/05  22    25   35
TD14 AST     28/02/07  92    30   90   Alb     26/02/07  15    25   35
Formatted data looks like this
So far I have tried:
data_wide2 <- dcast(tdl, SN + LabDate ~ LabCode, value.var="Result")
and
melt(tdl, id = c("SN", "LabDate"), measured= c("Result", "Upper", + "Lower"))
 
    