voyages =(
VIC0016,
VIC0016,
VIC0016,
VIC0016,
VIC0016,
VIC0016,
Truck,
VIC0016,
VIC0016,
VIC0016,
JUL0983,
BB11356,
VIC0022,
VIC0022,
ISK1981,
ISK1981,
ISK1981,
ISK1981,
ISK1981,
ISK1981,
ISK1981,
ISK1981,
ISK1981,
ISK1981,
ISK1981,
ISK1981)
clusters = (5,
5,
5,
4,
4,
4,
1,
3,
4,
3,
5,
2,
4,
5,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6,
6)
>calculate.confusion <- function(voyages, clusters)
{
d <- data.frame(voyages, clusters)
td <- as.data.frame(table(d))
# convert the raw counts into percentage of each voyage number
pc <- matrix(ncol=max(clusters),nrow=0)
for (i in 1:11) # 11 different voyage numbers
{
total <- sum(td[td$voyages==td$voyages[i],3])
#,3 is the third column, showing the frequencies
pc <- rbind(pc, td[td$voyages==td$voyages[i],3]/total)
}
rownames(pc) <- td[1:11,1]
colnames(pc)<-1:11
return(pc)
}
有了上面的数据框(数字是百分比),我怎样才能用行的名称替换列名 [1:11],这样:
- 在该行内,该行中百分比最高的列以该行命名
- 每个行名都使用一次
希望有人能帮助我。