# Print calculated values with headers after comparison

Asked by At

How can I print the header of values after I compare them? I have some calculated values stored as per below:

``````Value1 12
Value2 6
Value3 7
Value4 5
``````

How can I print the max among four values with the header?

I can only get the max among the values but not the header/description For e.g.:

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

gives me:

``````"12"
``````

but I need the output as:

``````"Max. value is Value1 = 12"
``````

Is this possible?

## 2 Answers On Best Solutions

Assuming the values are stored as single numeric objects as below, and named with some pattern `"ValueX"`, we can put them all into a list, then get index for max/min and `paste`:

``````# 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 ])
#  "Max. value is Value1 = 12"
`````` On

Possible solution could be this :

``````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 = " "))
``````