I have the following table:
Class x2 x3 x4
A 14 45 53
A 8 18 17
A 16 49 20
B 78 21 48
B 8 18 5
I need for each "Class" (A and B) find the maximum value in column "X3", keep that row and delete other rows.
The output should be in format like:
Class x2 x3 x4
A 14 49 20
B 78 21 48
Please, ask me questions if something unclear in my problem.
Thank you!
Try:
One way using
dplyr
would be:EDIT Given @Ananda's tie-values consideration and his suggestion in coments, you could do something like this as well. But, @Richard Acriven's idea is the way to go, if there are ties.
A base R approach could be:
However, note that if there are multiple values tied for
max
, it would return multiple rows for that group.Here's a possible "data.table" approach:
Here's another
dplyr
answer for the lotWhich could also be