Loop R to find the highest value in a column

Asked

Viewed 942 times

0

I would like a help in the code below. I have in the object df_ar a column called litros_cerveja, with the consumption of the drink in the countries of South America in the year 2010. I would like to save in the object menor_cerveja the lowest consumption in this column, together with the country, is presented in column with this name.

for (i in nrow(df_arl$litros_cerveja)){
    if (df_ar$litros_cerveja[i] < df_ar$litros_cerveja[i - 1]){
      menor_cerveja <- data.frame(df_ar$pais[i], df_ar$litros_cerveja[i])
    }
}
  • Has to be for(i in 2:nrow(...))

2 answers

1

You don’t need to create the function to find the lowest value, they already exist:

min() # para retornar o menor valor
which.min() # para retornar a posição do menor valor

in your case it would be something like this:

pos <- which.min(df_ar[, 'litros_cerveja']) # retornar posição do menor valor
menor_cerveja <- df_ar[pos, c('pais', 'litros_cerveja')] 
  • thanks my friend. I didn’t know which function.min().

1


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
  • then the function which(x == min(x)) uses logical value. Case TRUE, j <- which(x == min(x))... right?

  • @vinifrm x == min(x) compares each value of x with min(x) and returns logical values. Then, which transforms logical values into numerical indices. For example, try which(c(TRUE, FALSE, TRUE)). The which should be used when there is a possibility of NAs in the data, index with which(c(TRUE, FALSE, NA, TRUE)) is safe but with c(TRUE, FALSE, NA, TRUE) is not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.