I loaded a table like this:
V1 V2 V3
pat1 1 2
pat1 3 1
pat1 4 2
pat2 3 3
pat3 1 4
pat3 2 3
and I need to format it into something like the following, with V1 indicating the row, V2 indicating the column, and the values in V3:
1 2 3 4
pat1 2 0 1 2
pat2 0 0 3 0
pat3 4 3 0 0
Please, note that pat1 vs. pat2 vs. pat3 have different numbers of observations and that missing values must be filled with 0.
Using dcast
from reshape2
:
library(reshape2)
dcast(dat,V1~V2,fill=0)
V1 1 2 3 4
1 pat1 2 0 1 2
2 pat2 0 0 3 0
3 pat3 4 3 0 0
Where dat is :
dat <- read.table(text='V1 V2 V3
pat1 1 2
pat1 3 1
pat1 4 2
pat2 3 3
pat3 1 4
pat3 2 3',header=TRUE)