I asked this question a couple days ago and @Juan Bosco helped me and suggested the code which works perfectly and selects top n values from each column. But turns out I need the names of each selected row for each column, so in the list: "Selectedrows", I need something like "t9", "t8", "t7" instead of row number.
names<- c("t1","t10","t11","t2","t3","t4","t5","t6","t7","t8","t9")
values1 <- c(2,3.1,4.5,5.1,6.5,7.1,8.5,9.11,10.1,11.8,12.3)
values2 <- c(1,3.1,3,5.1,6.5,7.1,8.5,9.11,10.1,12,12)
mydf<- data.frame(names,values1,values2)
Selectedrows<- lapply(2:3, function(col_index) {
max_values <- sort(mydf[[col_index]], decreasing = T)[1:3]
max_rows <- sapply(max_values, function(one_value){
as.numeric(rownames(mydf[mydf[[col_index]] == one_value, ]))
})
unique(unlist(max_rows))[1:3]
})
Thanks