Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-如何让max()返回变量名而不是变量的内容?_R_Max_Variable Names - Fatal编程技术网

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

R-如何让max()返回变量名而不是变量的内容?,r,max,variable-names,R,Max,Variable Names,我需要从变量列表中找到最大值。但是,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 +

我需要从变量列表中找到最大值。但是,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
jan您可以尝试:

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

我还尝试将标签添加到列表中,但这些标签没有与max()一起使用。您可以先命名向量元素,例如:
jan谢谢您的回复!谢谢你,这很好用。我曾尝试过子集设置,但显然出了点问题…@jdfinch3很高兴知道它能起作用。你能在你的帖子中显示子集的代码吗?对不起,它已经被删除很久了,因为它不起作用。我想我把订单倒过来了。谢谢。:)