I have an input like this .
type1 type2 2
type1 type3 4
type1 type5 3
type2 type1 6
type2 type4 2
type2 type3 3
type3 type1 2
type3 type2 2
type3 type4 4
type4 type1 7
type4 type2 1
type4 type3 4
type5 type1 2
type5 type3 3
type5 type4 1
Here column1 is suppose one state and column two is second state and the third column has values corresponding to this transition. So I want it to be spread such that I have unique states in column1 and the rest of the columns are name with all unique column names and each cell in rows should contain the counts from third column.
So the output should look like this.
types type1 type2 type3 type4 type5
type1 0 2 4 0 3
type2 6 0 3 2 0
type3 2 2 0 2 0
type4 7 1 4 0 0
type5 2 0 3 1 0
I have tried this which gives error : Duplicate identifiers for rows (7, 8). I don't know how to use spread in this case.
seq=read.csv("test.txt",header=FALSE,sep="\t")
colnames(seq) = c("state1","state2","counts")
seqs=spread(data=seq,state1,state2,fill=0)
Any help is appreciated.