[R] re shaping a data frame with multiple IDs

Christian Pilger christian.pilger at gmx.net
Sun Mar 8 15:25:09 CET 2009


Dear R-experts,

I have the following "long" data frame:

ID       <-
c("ID001","ID001","ID001","ID001","ID001","ID001","ID001","ID001","ID001","ID001","ID001","ID002","ID002","ID002","ID002","ID002","ID002","ID002")
Var      <-
c("Var001","Var001","Var001","Var001","Var001","Var002","Var002","Var002","Var002","Var002","Var002","Var001","Var001","Var001","Var001","Var002","Var002","Var002")
Dose     <-
c("100","100","80","80","50","100","100","100","80","50","25","100","100","80","50","100","80","50")
Response <-
c("90%","15%","90%","90%","90%","100%","100%","90%","100%","100%","70%","90%","15%","90%","90%","100%","100%","100%")

DF <- data.frame(ID,Var,Dose,Response)
DF

      ID    Var Dose Response
1  ID001 Var001  100      90%
2  ID001 Var001  100      15%
3  ID001 Var001   80      90%
4  ID001 Var001   80      90%
5  ID001 Var001   50      90%
6  ID001 Var002  100     100%
7  ID001 Var002  100     100%
8  ID001 Var002  100      90%
9  ID001 Var002   80     100%
10 ID001 Var002   50     100%
11 ID001 Var002   25      70%
12 ID002 Var001  100      90%
13 ID002 Var001  100      15%
14 ID002 Var001   80      90%
15 ID002 Var001   50      90%
16 ID002 Var002  100     100%
17 ID002 Var002   80     100%
18 ID002 Var002   50     100%

>From this I wish to generate a "wide" data frame (note: repetitions at
identical "Dose" are given indepently for each "ID"):

ID	Dose	Var001.Response	Var002.Response
ID001	100	90%	100%
ID001	100	15%	100%
ID001	100	NA	90%
ID001	80	90%	100%
ID001	80	90%	NA
ID001	50	90%	100%
ID001	25	NA	70%
ID002	100	90%	100%
ID002	100	15%	NA
ID002	80	90%	100%
ID002	50	90%	100%

I tried 

reshape(DF, timevar="Var", idvar=c("ID", "Dose"), direction="wide")

and obtained:

      ID Dose Response.Var001 Response.Var002
1  ID001  100             90%            100%
3  ID001   80             90%            100%
5  ID001   50             90%            100%
11 ID001   25            <NA>             70%
12 ID002  100             90%            100%
14 ID002   80             90%            100%
15 ID002   50             90%            100%

This table is close to what I need, but it lacks entries if more than one
"Response" exists for a given "Dose".

Where is my mistake?

Best wishes

Christian

-- 
View this message in context: http://www.nabble.com/reshaping-a-data-frame-with-multiple-IDs-tp22398496p22398496.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list