【发布时间】:2015-12-11 15:30:42
【问题描述】:
我有这样的输入。
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
这里第一列是假设一个状态,第二列是第二个状态,第三列有对应于这个转换的值。所以我希望它被传播,这样我在 column1 中有唯一的状态,其余的列是具有所有唯一列名的名称,并且行中的每个单元格都应该包含第三列的计数。
所以输出应该是这样的。
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
我试过这个,它给出了错误:行 (7, 8) 的标识符重复。我不知道在这种情况下如何使用传播。
seq=read.csv("test.txt",header=FALSE,sep="\t")
colnames(seq) = c("state1","state2","counts")
seqs=spread(data=seq,state1,state2,fill=0)
感谢任何帮助。
【问题讨论】:
-
你几乎拥有它。试试
spread(seq, state2, counts, fill = 0) -
xtabs(counts~., df) -
@docendo discimus 。我之前尝试过并更新了我的帖子,但它出错了,因为它不适用于不同的更大数据。
标签: r