group - Count unique data point in grouped data in R -


this question has answer here:

i've been searching hours on , cannot find solution.

i have below data set:

   id   rank      1   1            1   3            1   3            1   4            2   2            2   3            2   3            2   3            3   1            4   2            4   2        

i'm trying group id , produce count of how many unique times rank shows within group id results

   id   rank  unique     1   1       1     1   3       2     1   3       2     1   4       1     2   2       1     2   3       3     2   3       3     2   3       3     3   1       1     4   2       2     4   2       2 

i'm using formula , it's providing result of in rank column

unique <- with(data, rank, id, fun = function(i) length((unique(i)))) 

any extremely appreciated!

base r solution. tabulates number of items cross-classified id , rank. ave function useful when can identify 1 vector need f=unction applied within possibly multiple criteria need result distributed dataframe: length function doing counting:

> dat$unique <- with(dat, ave(id, id, rank, fun=length)) > dat    id rank unique 1   1    1      1 2   1    3      2 3   1    3      2 4   1    4      1 5   2    2      1 6   2    3      3 7   2    3      3 8   2    3      3 9   3    1      1 10  4    2      2 11  4    2      2 

when @hadley decided redefine r in dplyr, used count name of equivalent function.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo