Search code examples
rsortingvectormeannames

R: Sorting vectors by mean in decreasing order while printing the vector names


This is a very basic question. For an assignment, I need to compare the volumes of two bottle-filling machines. Essentially, I want to know which of the two machines fills to a greater volume, so I'm comparing two means based on 10 observations for each machine.

machine1 <- c(151.2,150.5,149.2,147.5,152.9,152.0,151.3,149.7,149.4,150.7)
machine2 <- c(151.9,151.4,150.3,151.2,151.0,150.2,151.2,151.4,150.4,151.7)
means <- c(mean(machine1), mean(machine2))
sort(means, decreasing=T) [1]

As you'd expect, the output of this is the greatest vector mean, which happens to be the mean of machine2:

[1] 151.07

I'd like the output to be the name of the vector (i.e. machine2). If I have a large number of vectors, I want to simply know the name of the vector with the greatest mean, without having to print each vector mean individually and comparing them myself. I've tried using the names() command but can't figure out how to integrate it here.


Solution

  • We can get the objects in a list and then use which.max

    names(which.max(sapply(mget(ls(pattern = 'machine\\d+')), mean)))
    #[1] "machine2"
    

    The ls returns all the objects in the global environment as a string with the pattern specified i.e. 'machine' followed by one or more digits (\\d+), mget gets the value of the objects in a list, loop through the list with sapply, get the mean as a vector which also have the names as the object name. The which.max gets the index of the maximum mean and wrapping with names returns the name of the index