Counting unique / distinct values by group in a data frame

前端 未结 11 2151
终归单人心
终归单人心 2020-11-22 00:12

Let\'s say I have the following data frame:

> myvec
    name order_no
1    Amy       12
2   Jack       14
3   Jack       16
4   Dave       11
5    Amy             


        
11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 00:26

    This would also work but is less eloquent than the plyr solution:

    x <- sapply(split(myvec, myvec$name),  function(x) length(unique(x[, 2]))) 
    data.frame(names=names(x), number_of_distinct_orders=x, row.names = NULL)
    

提交回复
热议问题