-2

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.

glennsl
  • 28,186
  • 12
  • 57
  • 75
soosus
  • 1,211
  • 4
  • 18
  • 27
  • with `dplyr`, `df %>% group_by(c1) %>% summarise(v2 = n_distinct(c2)) ` Or using base R `aggregate(c2~c1, df, function(x) length(unique(x)))` – Ronak Shah May 08 '19 at 06:35
  • @Ronak Shah: Sorry for my almost identical answer, I was identifying traffic lights in the recaptcha box which made my answer arrive late.. – Lennyy May 08 '19 at 06:42
  • @Lennyy No issues, one should always follow "traffic lights" ;-) – Ronak Shah May 08 '19 at 06:43

1 Answers1

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

  c1 c2
1  A  2
2  B  3
Lennyy
  • 5,932
  • 2
  • 10
  • 23