Counting unique / distinct values by group in a data frame

前端 未结 11 2170
终归单人心
终归单人心 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:33

    Few years old .. although had similar requirement and ended up writing my own solution. Applying here:

     x<-data.frame(
     
     "Name"=c("Amy","Jack","Jack","Dave","Amy","Jack","Tom","Larry","Tom","Dave","Jack","Tom","Amy","Jack"),
     "OrderNo"=c(12,14,16,11,12,16,19,22,19,11,17,20,23,16)
    )
    
    table(sub("~.*","",unique(paste(x$Name,x$OrderNo,sep="~",collapse=NULL))))
    
    
      Amy  Dave  Jack Larry   Tom
        2     1     3     1     2
    

提交回复
热议问题