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