r - Populate cell with the column name of the max value in corresponding row -


i practicing r programming skills using kaggle data sets, , use help. working on ghosts, ghouls, , goblins data set , goal predict type of monster each row represents based on set of descriptive stats. trained multinomial logistic regression model using training data set probability values each of 3 types, , want put name of monster in last cell of each row in test data set based on on max probability 3 columns in row. here head of table: predprobs table

what have tried seems populate every cell in type column same value. how can calculate max probability within columns "ghost", "ghoul", , "goblin", column name of column containing max value, , populate last cell in every row (column name: type) name? want every row in test data set. trying , cbind typeslist whole list called predprobs.

for (i in nrow(predprobs)) {typeslist = append(typeslist, which.max(apply(predprobs[i,7:9], margin = 2, max)))} 

but doesn't seem creating vector need. thoughts? similar post: find max value in row , update new column max column name but, unfortunately, i'm not fluent in sql yet i'm not able translate r.

any appreciated. thanks!

-wes

you should think of this:

 t(apply(predprobs,1,function(i)append(i,names(predprobs)[which.max(i)],length(i)))) 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -