Search code examples
runique

Take first entry in repeated measures in R


df:

ID     Cat1  Cat2    Cat3   Cat4
A0001   358 11.25   37428   0
A0001   279 14.6875 38605   0
A0013   367 5.125   40152   1
A0014   337 16.3125 38624   0
A0020   367 8.875   37797   0
A0020   339 9.625   39324   0

want an output to keep only the first unique entry by ID, i.e.

ID     Cat1  Cat2    Cat3   Cat4
A0001   358 11.25   37428   0
A0013   367 5.125   40152   1
A0014   337 16.3125 38624   0
A0020   367 8.875   37797   0

Appreciate any help on this.


Solution

  • The duplicated function returns TRUE for elements that have already appeared in a vector. Since you want the first occurrence of ID, just use

    df[!duplicated(df$ID),]