比较后用 headers 打印计算值

Print calculated values with headers after comparison

如何在比较值后打印 header 个值? 我有一些计算值存储如下:

Value1 12
Value2 6
Value3 7
Value4 5

如何使用 header 打印四个值中的最大值?

我只能得到最大值,不能得到 header/description 例如:

"print(max(Value1,Value2,Value3,Value4))"

给我:

"12"

但我需要输出为:

"Max. value is Value1 = 12"

这可能吗?

假设值存储为如下单个数字对象,并以某种模式命名 "ValueX",我们可以将它们全部放入列表中,然后获取 max/min 和 [=12 的索引=]:

# example data
Value1 <- 12
Value2 <- 6
Value3 <- 7
Value4 <- 5

# put all in a named vector
x <- mget(ls()[ grepl("^Value", ls()) ])

# then paste
ix <- which.max(x)
paste("Max. value is", names(x[ ix ]), "=", x[ ix ])
# [1] "Max. value is Value1 = 12"

可能的解决方案是这样的:

c <- data.frame(a = c("Value1", "Value2", "Value3"), b = c("4","9","5"), stringsAsFactors = FALSE)

print(paste("Max. value is", str_c(c[which(c$b == max(c$b)), 1], c[which(c$b == max(c$b)), 2], sep = " = "), sep = " "))