gpt4 book ai didi

R - 如何让 max() 返回变量名而不是变量的内容?

转载 作者:行者123 更新时间:2023-12-04 23:21:10 25 4
gpt4 key购买 nike

我需要从变量列表中找到最大值。但是,max() 返回变量的内容而不是变量名。有没有办法获取名称而不是内容?

快速示例代码:

jan <- 0
feb <- 0
mar <- 0

#for testing purposes - just select a random month and add 10
s1 <- sample(1:3, 1)
if (s1==1) {
jan <- jan + 10
}
if (s1==2) {
feb <- feb + 10
}
if (s1==3) {
mar <- mar + 10
}

final <- max(jan, feb, mar)

final

结果总是 10。这没有帮助......有没有办法让月份/变量名称返回? (即“jan”而不是“10”)

谢谢!

最佳答案

你可以试试:

 c("jan", "feb", "mar")[which.max(c(jan, feb, mar))]
#[1] "jan"

关于R - 如何让 max() 返回变量名而不是变量的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26448529/

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