My question is related to Extract the maximum value within each group in a dataframe.
The question there is essentially: how do you select the max value in one column based on repeated groups in a separate column in the same data frame?
In that post, user EDi provides a ton of examples for how to accomplish this task.
My question: how do I accomplish the same task, but instead of reporting the max value, I instead report a value in a third column associated with that max value?
For example:
Assume I have a data.frame:
Group Value Year A 12 1933 A 10 2010 B 3 1935 B 5 1978 B 6 2011 C 1 1954 D 3 1933 D 4 1978
For each level of my grouping variable, I wish to extract the year that the maximum value occurred. The result should thus be a data frame with one row per level of the grouping variable:
Group Year A 1933 B 2011 C 1954 D 1978
I know I could use any of the answers from EDi's post mentioned above and then just use something like which
,match
or sapply
to figure out the year, but that seems too sloppy.
Is there a quick way to extract a value in column A given the maximum value in column B within each group (column C) in a dataframe?
Update: Could someone please provide a base R solution?