I would like to clean up the following code. Specifically, I\'m wondering if I can consolidate the three filter statements so that I end up with the final data.frame (the ri
It looks like within each group of f
, you want to extract the row of, in descending order of preference, spring
, fall
, or other
.
If you first make your ordering of preference the actual factor ordering:
dat0$s <- factor(dat0$s, levels=c("spring", "fall", "other"))
Then you can use this dplyr solution to get the minimum row (relative to that factor) within each group:
newdat <- dat0 %.% group_by(f) %.% filter(rank(s) == 1)