r - How to get column name of the variable with the top 10 highest values? -
if have data.frame(sum_clus) 600 columns(variables) , 10 rows have no na's , numeric values, how can create 5 new variables give me column names of top 5 variables in row?
for eg.
max <- apply(sum_clus ,1, max) for(ii in 1:10) sum_clus$max[ii] <- colnames(sum_clus)[which(sum_clus[ii , ] == sum_clus[ii, sum_clus[ii,] == max[ii]])]
this above code helped me create variable sum_clus$max gives me column name of max variable in each row. similarly, how can 5 such variables give me column names of top 5 variables? sum_clus$max, sum_clus$second_but_max, , on..
thanks in advance!
here's similar solution, using (i) loop instead of apply
; , (ii) rank
instead of order
.
set.seed(1) n_i = 10 n_ii = 600 n_top = 5 df <- data.frame(matrix(runif(n_ii*n_i), ncol = n_ii)) out <- matrix("",n_top,n_i) (i in 1:n_i){ colranks <- rank(df[i,]) out[,i] <- names(sort(colranks)[n_ii:(n_ii-(n_top-1))]) } # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] # [1,] "x369" "x321" "x348" "x415" "x169" "x258" "x55" "x182" "x99" "x78" # [2,] "x42" "x295" "x563" "x173" "x377" "x31" "x246" "x353" "x259" "x384" # [3,] "x98" "x440" "x371" "x207" "x429" "x292" "x433" "x437" "x123" "x558" # [4,] "x13" "x193" "x396" "x78" "x543" "x228" "x211" "x2" "x583" "x508" # [5,] "x35" "x364" "x249" "x33" "x388" "x405" "x458" "x252" "x569" "x456"
the one-liner analogue apply
is
apply(df,1,function(x)names(sort(rank(x))))[600:596,]
Comments
Post a Comment