I have an use-case that I have not came across before. I have the following data frame and would like to select values of "y" where "x" achieves its minimum and maximum respectively for each level of the condition "i".
> library(dplyr)
> df <- data.frame(i=c(1,1,2,2),x=c(1.0,2.0,3.0,4.0),y=c('a','b','c','d'))
> ddply(df, .(i), summarise, Min=min(x), Max=max(x))
i Min Max
1 1 2
2 3 4
which is correct but I'd like to instead have the y
whose x
is Min
or Max
.
i Min Max
1 a b
2 c d
How can I do that?