Say I have a dataframe df:

c1   c2
A    porcupine
A    snail
A    snail
B    porcupine
B    snail
B    goose

I want to use this dataframe to generate another that reports the counts of different values in c2 that correspond to a unique string in c1. So, given the example above, it would look something like this:

v1   v2
A    2
B    3

since there were only two different values in c2 that had "A" as the string in c1 and there were three different values in c2 that had "B" in c1.

I'm guessing I need to use sapply but I'm not sure how.

1 Answers

0
Lennyy On
aggregate(c2 ~ c1, df, function(x) length(unique(x)))

  c1 c2
1  A  2
2  B  3