How to randomly delete observations according to ID

I have a dataset where there are multiple observations per ID. Some have 1, 2, or 3 observations per ID. I want to randomly drop one observation for every ID if that ID has more than 1 observation.

Additionally, how would I do this if I want drop 2 observation for those IDs that have a total of 3 observations?

library(tidyverse)

myframe <- expand.grid(value=1:5,id=1:3)

# keep only 1 random row of value per b

myframe$randval <- runif(nrow(myframe))

myframe <- arrange(myframe,id,randval)

myfinal <- group_by(myframe,
                    id) %>%
            top_n(n = 1)

myfinal

Thank you so much! This helped me get them all down to 1 observation per ID.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.