独立选择每一列而不是行号时如何保存行名? [英] How to save row names when selecting each column independently instead of row number?

查看:22
本文介绍了独立选择每一列而不是行号时如何保存行名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前我问了这个问题,@Juan Bosco 帮助了我并建议了完美运行并从每列中选择前 n 个值的代码.但结果我需要每一列的每个选定行的名称,所以在列表中:Selectedrows",我需要像t9"、t8"、t7"这样的东西而不是行号.

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]

})

谢谢

推荐答案

在这种情况下,您可以使用 order,它返回可用于对数组进行排序的索引,前三个元素如果您指定要递减的数组,则 order 结果对应于前 3 个值的索引;使用索引,您可以对 names 列进行子集化并获得相应的值:

In this case, you can use order, which returns the index which can be used to sort an array, the first three elements from the order results corresponds to the index of the top 3 values if you specify the array to be decreasing; With the index, you can subset the names column and get the corresponding values:

lapply(2:3, function(col_index) {
    mydf[["names"]][order(mydf[[col_index]], decreasing = T)[1:3]]
})

#[[1]]
#[1] "t9" "t8" "t7"

#[[2]]
#[1] "t8" "t9" "t7"

请注意,如果不是,则假定您的名称列是字符.如果您希望结果是字符而不是因子,请首先执行 mydf$names <- as.character(mydf$names).

Notice this assumes your names column is character, if not. Do mydf$names <- as.character(mydf$names) firstly if you want the result to be character instead of factor.

这篇关于独立选择每一列而不是行号时如何保存行名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆