Randomly select groups (and all cases per group) in R?

后端 未结 2 1014
孤街浪徒
孤街浪徒 2021-01-15 02:02

I have an R dataframe with two levels of data: id and year. Within groups defined by id, the years increase (entire dataset has the sa

2条回答
  •  攒了一身酷
    2021-01-15 02:26

    subset(df, id %in% sample(levels(df$id), 20))
    

    that's assuming your data frame is called df and that your id is a factor (use unique instead of levels if it's not)

提交回复
热议问题