3

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

Community
  • 1
  • 1
Elbert
  • 516
  • 1
  • 5
  • 15
  • 1
    try this: `table(asa)` – Adam Quek May 10 '16 at 09:30
  • can I set a variable with value of n of aa? – Elbert May 10 '16 at 09:33
  • 1
    huh? put it in a data.frame like `asad <- data.frame(table(asa))`, and change the name e.g. `names(asad)<-c("asa", "n")` – Adam Quek May 10 '16 at 09:36
  • 1
    The reason why your simple copy/paste from the other solution does not work is that in your example data, you're counting the same variable by which you are grouping. Therefore, `unique(asa)` results in a length 1 vector. The data in the post you referred to grouped by name and then counted by another variable. Thus in your case just remove the `unique()` and you're good to go. – Paul Lemmens May 10 '16 at 12:01

2 Answers2

2

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
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Even more simple, just use the as.data.frame and table functions with no other parameters.

as.data.frame(table(df$asa))
Colonel_Old
  • 852
  • 9
  • 15