-1

Hi I'd like to aggregate by date and unique values of such that:

Date        Client_id       Purchase
01-01-2016    00001          Wine
01-01-2016    00001          Beer
01-01-2016    00002          Wine
02-01-2016    00003          Beer
02-01-2016    00004          Wine
03-01-2016    00005          Beer

So I would have something like:

Date        Number of Clients
01-01-2016     2
02-01-2016     2
03-01-2016     1

I am trying with dplyr and base R aggregate function but I haven't succeeded:

daily_customers <- df %>% sum(date) %>% unique(Client_id)
daily_customers <-  aggregate(Date~ unique(client_id))

Any suggestion?

adrian1121
  • 904
  • 2
  • 9
  • 21

3 Answers3

1
library(dplyr)
df %>% group_by(Date) %>% summarise("Number of Clients" = length(unique(Client_id)))  

library(data.table)
df[ , .("Number of Clients" = length(unique(Client_id))), by = .(Date)]

#        Date Number of Clients
#1 01-01-2016                 2
#2 02-01-2016                 2
#3 03-01-2016                 1
joel.wilson
  • 8,243
  • 5
  • 28
  • 48
1

In dplyr, you can also use n_distinct() instead of using length(unique())

df %>%
group_by(Date) %>%
summarise(nOfClients = n_distinct(Client_id))

#        Date nOfClients
#      <fctr>      <int>
#1 01-01-2016          2
#2 02-01-2016          2
#3 03-01-2016          1
jazzurro
  • 23,179
  • 35
  • 66
  • 76
0
> library(plyr)
> count(x,'Date')

        Date freq
1 01-01-2016    3
2 02-01-2016    2
3 03-01-2016    1
harshil9968
  • 3,254
  • 1
  • 16
  • 26