gpt4 book ai didi

r - 如何获取具有前 10 个最高值的变量的列名?

转载 作者:行者123 更新时间:2023-12-04 11:09:55 24 4
gpt4 key购买 nike

如果我有一个包含 600 列(变量)和 10 行的 data.frame(sum_clus),其中没有 NA 并且都是数值,那么我如何创建 5 个新变量来为我提供该行中前 5 个变量的列名?

例如。

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]])]

上面的代码帮助我创建了一个变量 sum_clus$max,它为我提供了每行中 max 变量的列名。同样,我怎样才能得到 5 个这样的变量,这些变量给了我前 5 个变量的列名? sum_clus$max, sum_clus$second_but_max, 等等..

提前致谢!

最佳答案

一种选择是使用 order() 然后使用它来对列名进行子集化,例如:

set.seed(1)
df <- data.frame(matrix(runif(600*10), ncol = 600))

foo <- function(x, names) {
ord <- order(x, decreasing = TRUE)[1:5]
names[ord]
}

nams <- colnames(df)
apply(df, 1, foo, names = nams)

生产
> apply(df, 1, foo, names = nams)
[,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"

检查这是否有效:
> names(sort(unlist(df[1,, drop = TRUE]), decreasing = TRUE)[1:5])
[1] "X369" "X42" "X98" "X13" "X35"
> names(sort(unlist(df[2,, drop = TRUE]), decreasing = TRUE)[1:5])
[1] "X321" "X295" "X440" "X193" "X364"

看起来还可以。

关于r - 如何获取具有前 10 个最高值的变量的列名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16551258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com