spore234 spore234 - 23 days ago 9
R Question

translate data frame of observations into ranks

I have a data set like this:

df <- data.frame(situation1=rnorm(30),
situation2=rnorm(30),
situation3=rnorm(30),
models=c(rep("A",10), rep("B",10), rep("C", 10)))


where I compare three models (A,B,C) in three situations. I have 10 measurements for each model.

I now want to summarise this into ranks, i.e. how often each models wins in each situtation. Win is defined by the highest value.

A final output could be something like this:

model situation1 situtation2 situtation3
A 4 3 3
B 7 1 2
C 1 4 5

Answer

In base R:

table(df$models,colnames(df[-4])[max.col(df[-4])])
#    situation1 situation2 situation3
#  A          2          4          4
#  B          4          5          1
#  C          2          4          4

Results may change from your OP, since you didn't set a seed.