Using order and unique to Eliminate Pseudoreplication

In this rather more complicated example, you are asked to extract a single record for each vegetation type, and that record is to be the case within that vegetation type that has the greatest worm density. There are two steps to this: first order all of the rows of the dataframe using rev(order(Worm.density)), then select the subset of these rows which is unique for vegetation type:

worms[rev(order(Worm.density)),][unique(Vegetation),]

         Field.Name   Area   Slope   Vegetation   Soil.pH   Damp  Worm.density
16     Water.Meadow    3.9       0       Meadow       4.9   TRUE             8
9       The.Orchard    1.9       0      Orchard       5.7  FALSE             9
11      Garden.Wood    2.9      10        Scrub       5.2  FALSE             8
2    Silwood.Bottom    5.1       2       Arable       5.2  FALSE             7
10    Rookery.Slope    1.5       4    Grassland       5.0   TRUE             7

Get The R Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.