Despite the answer by Willian Vieira be, in a certain sense, correct, it should be noted that maybe it is not the solution for all cases.
The function which.min
(and the corresponding which.max
) returns the first the minimum value of the vector (respectively, of the maximum of the vector). If there is more than a minimum then, to obtain all, should be otherwise.
x <- c(3, 1, 2, 5, 1, 4)
which.min(x)
#[1] 2
x[which.min(x)]
#[1] 1
which(x == min(x))
#[1] 2 5
x[which(x == min(x))]
#[1] 1 1
Now, the question is asked
I would like to save on the object menor_cerveja
the lowest consumption in this column, together with the country, is presented in column with this name.
Nothing guarantees that there is only one country with the lowest consumption of beer. I would say that very likely there are no ties but who knows? In the following fictitious case there are two countries with the same lower beer consumption.
df_ar <- data.frame(pais = letters[1:6],
litros_cerveja = c(3, 1, 2, 5, 1, 4))
i <- which.min(x)
df_ar[i, ]
# pais litros_cerveja
#2 b 1
j <- which(x == min(x))
df_ar[j, ]
# pais litros_cerveja
#2 b 1
#5 e 1
Has to be
for(i in 2:nrow(...))
– Rui Barradas