Select the highest value of a column set of a df and take the column name of that highest value in R

Asked

Viewed 1,088 times

2

Guys I have a table in the R in which I wanted to look for the highest value in a given set of columns and given this higher value, return in a new column the name of the column in which that higher value was.

For example, given the df below I wanted to look for the highest value of each row between columns of A to F and create a new column called colunaOriginal containing the name of the column in which the largest was.

For the first row the highest value would be 100 (in column A) and the information colunaA would be in the column colunaOriginal.

df_maior_valor <- 
structure(list(A = c(100, 49, 30, 11, 7, 0, 1, 5, 7, 12), B = c(50, 
51, 20, 10, 3, 10, 2, 6, 3, 3), C = c(0, 1, 5, 2, 5, 0, 0, 2, 
1, 1), D = c(0, 0, 1, 0, 1, 0, 0, 0, 3, 2), E = c(0, 0, 0, 0, 
1, 0, 0, 0, 1, 0), F = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0)), .Names = c("A", 
"B", "C", "D", "E", "F"), row.names = c(NA, 10L), class = "data.frame")

1 answer

4


Without using any extra packages, I would solve it as follows. First, I find out which column has the highest value. This response is given by the position of the column:

x <- apply(df_maior_valor, 1, which.max)
x
 1  2  3  4  5  6  7  8  9 10 
 1  2  1  1  1  2  2  2  1  1

Then I turn x in a factor.

x <- as.factor(x)

This factor will have numerical levels. Therefore, I need to name this levels as defined in the original problem:

levels(x) <- paste0("coluna", LETTERS[1:6])

x
      1       2       3       4       5       6       7       8       9      10 
colunaA colunaB colunaA colunaA colunaA colunaB colunaB colunaB colunaA colunaA 
Levels: colunaA colunaB colunaC colunaD colunaE colunaF

Next, I organize everything into a new data frame:

data.frame(df_maior_valor, colunaOriginal=x)
     A  B C D E F colunaOriginal
1  100 50 0 0 0 0        colunaA
2   49 51 1 0 0 0        colunaB
3   30 20 5 1 0 0        colunaA
4   11 10 2 0 0 0        colunaA
5    7  3 5 1 1 1        colunaA
6    0 10 0 0 0 0        colunaB
7    1  2 0 0 0 0        colunaB
8    5  6 2 0 0 0        colunaB
9    7  3 1 3 1 0        colunaA
10  12  3 1 2 0 0        colunaA
  • Hello Marcus Nunes when I tried to run that your resolution I received the following error Error in data.frame(df_maior_valor, colunaOriginal = x) : arguments imply differing number of rows: 10, 0

  • 1

    I drove here again and it worked yes! Vlw by help!

  • As I also asked in the Stack in English, which by the way had already been answered before (I couldn’t find it when I was searching), follow the link to another solution very similar to the one above https://stackoverflow.com/questions/46531396/search-the-biggest-number-in-range-of-colums-and-the-column-name-Where-this-Numb

Browser other questions tagged

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