I'm trying to get something simple using group_by, count & mutate without success.
I want an answer to - How many distinct invNo are there by custId?
The answer I'm after is -
custId invNo
1 2
2 2
Here are my attempts.
library (tidyverse)
df = data.frame (
custId = c(1, 1,1,1,2,2,2,2),
invNo = c(201, 201,202,202,301,301,302,302),
item = c('a', 'b', 'a', 'c','a', 'b', 'a', 'c'),
amount = c(10, 20, 10, 30, 10, 20, 10, 30)
)
glimpse(df)
df
df %>% count(item)
df %>% count(custId,invNo)
df %>% count(invNo, custId)
df %>% group_by(custId) %>% count(invNo) %>% distinct()
df %>% group_by(custId) %>% mutate(unity=1) %>% summarize(Freq=n(), ans=sum(unity)) %>% distinct()
```{r}