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")
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
– Garbes
I drove here again and it worked yes! Vlw by help!
– Garbes
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
– Garbes