0

I have a dataset with multiple columns of scores I would rank from 1 to 10.

My ds is like this

     col1  col2
AA   5.64  2.33
BB   8.95  12
Cc   1.3   0.3
DD   2.55  7.66
EE   19.2  9.555
FF   88    42
GG   88    12

My expected result is

   col1  col2
AA   3    2
BB   4    5
CC   1    1
DD   2    3
EE   5    4
FF   6    6
GG   6    5

Attacked the df of scores and the wrong output

enter image description here enter image description here

I tried this, where rankCols is the name of columns (col1, col2) and x is the name of rows (AA, BB, CC, DD) with the values.

setDT(df)[, c(rankCols):=lapply(.SD, \(x) frank(-x)), .SDcols=rankCols]

but the result is not really working, jumping, for examples from 4 to repeated 7s. I have no idea why.

This is my script. "matrixes" is a list containing 3 data.frame

for(gg in 1:length(matrixes)){
df=data.frame(t(matrixes[[gg]]))  #trasponse the data.frame
names=rownames(df)
rankCols <- colnames(df)
setDT(df)[, c(rankCols):=lapply(.SD, \(x) frank(-x)), .SDcols=rankCols]  #this I think does not work
df=as.data.frame(df)
rownames(df) = names
dflist[[gg]]= df  #this save the df in a new list named dflist
}
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Ele14
  • 1

0 Answers0