-1
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)  
}  

Having the above data frame (numbers are percentages), how can I replace the column names [1:11] by the names of the rows, in such a way that:

  • within the row, the column having the highest percentage in that row is named after that row
  • every row name is used once

Hope somebody can help me.

M--
  • 25,431
  • 8
  • 61
  • 93
Fleur Lolkema
  • 35
  • 1
  • 3

1 Answers1

0

This should help:

# sample data
df <- data.frame(a = c(1,2,3), b = c(3,2,1), c = c(2,3,1))
colnames(df)
# [1] "a" "b" "c"
for(i in 1:nrow(df)) {colnames(df)[df[i, ] == max(df[i, ])] <- rownames(df)[i]}
colnames(df)
# [1] "3" "1" "2"
symbolrush
  • 7,123
  • 1
  • 39
  • 67
  • Dear, when I try this, not al the voyages numbers are used and some are used twice. Besides, the column containing the highest percentage is not always given the name of that voyage. Is there a way to solve this problem? – Fleur Lolkema May 14 '18 at 09:28
  • There is always a way.. Please try to ask a more specific question for a more specific answer (providing a standalone running example). See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – symbolrush May 14 '18 at 10:02