Take the subsets of a data.frame with the same feature and select a single row from each subset

前端 未结 3 944
执念已碎
执念已碎 2020-12-11 12:50

Suppose I have a matrix in R as follows:

ID Value
1 10
2 5
2 8
3 15
4 7
4 9
...

What I need is a random sample where every element is repre

3条回答
  •  囚心锁ツ
    2020-12-11 13:24

    You can do that with dplyr like so:

    library(dplyr)
    df %>% group_by(ID) %>% sample_n(1)
    

提交回复
热议问题