Search code examples
rdataframecountplyrdistinct-values

Counting distinct values in column of a data frame in R


So i would like to compute a distinct value of a column. This is the data frame :

asa
----
aa
bb
aa
aa
bb
cc
dd

Want to get :

asa |  n
--------
aa  |  3
bb  |  2
cc  |  1
dd  |  1

I ve tried using ddply from Counting unique / distinct values by group in a data frame and do this code : (reproducible)

asa<-c("aa","bb","aa","aa","bb","cc","dd")
asad<-data.frame(asa)
ddply(asad,~asa,summarise,n=length(unique(asa)))

But I got :

  asa n
1  aa 1
2  bb 1
3  cc 1
4  dd 1

It didnt do the computation. Notes that the value in column can be added anytime. so it is not always "aa","bb","cc",and "dd". Also it can be separated by space or comma ("aa bb" , "aa,bb" or "aa, bb") There must be a way for this. thank you in advance


Solution

  • We can use table

    setNames(as.data.frame(table(df1$asa)), c("asa", "n"))
    #   asa    n
    #1   aa    3
    #2   bb    2
    #3   cc    1
    #4   dd    1
    

    Or with tally from dplyr

    library(dplyr)
    df1 %>%
         group_by(asa) %>% 
         tally()
    #    asa     n
    #   (chr) (int)
    #1    aa     3
    #2    bb     2
    #3    cc     1
    #4    dd     1