amh
amh

Reputation: 690

Finding maximum value for column among a subset of a data frame

Given a data frame df with columns d, c, v. How do I find the value of d for the maximum value of v among the subset of records where c == "foo"?

I tried this:

df[df$v==max(df$v) & df$c == "foo","d"]

But I got:

character(0)

Upvotes: 4

Views: 7679

Answers (2)

jim
jim

Reputation: 906

While Manuel's answer will work most of the time, I believe a more correct version would be:

with(df, d[v== max(v[c=="foo"]) & c=="foo"])

Otherwise it's possible to match a row which has v==max but is not in fact a subset of c=="foo".

Upvotes: 1

Manuel Ramón
Manuel Ramón

Reputation: 2498

Yo can do as follows:

with(df, d[v== max(v[c=="foo"])])

EDITED: If you want to get the value of d for all the levels of c:

library(plyr)
ddply(df, "c", subset, v==max(v))

Upvotes: 9

Related Questions