I have columns of a matrix with the names of users' unique ID's. Each row in the column represents a risk factor score based on a regression model that predicted risk. I want to report each individual's top 2 risk factors (both the row name representing the factors and the actual values) along with their ID.
I've searched for solutions and found a lot of near misses. For instance, max
would work if I only wanted the top 1 value without a row name, and it I didn't have individuals as columns then I think that I could use some variation of sort(x,partial=n-1)[n-1]
.
My data looks like this:
ID.H00034222 ID.H00034305
a.score 0.040093810 0.04009381
b.score -0.038265220 -0.03826522
c.score 0.044418130 0.04441813
d.score -0.418624640 -0.05656504
e.score -0.005192439 0.10851938
f.score 0.005026030 0.02174170
df <- structure(c(0.04009381, -0.03826522, 0.04441813, -0.41862464,
-0.005192439, 0.00502603, 0.04009381, -0.03826522, 0.04441813,
-0.05656504, 0.10851938, 0.0217417), .Dim = c(6L, 2L), .Dimnames = list(
c("a.score", "b.score", "c.score", "d.score", "e.score",
"f.score"), c("ID.H00034222", "ID.H00034305")))
My output should give a user ID, and highest 2 row names and values for each ID.