按日期和唯一组聚合

时间:2017-01-09 11:34:21

标签: r dplyr aggregate

您好我希望根据日期和唯一值汇总:

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

所以我会有类似的东西:

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

我正在尝试使用dplyr和基本R聚合函数,但我还没有成功:

FILE*

有什么建议吗?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

在dplyr中,您也可以使用n_distinct()代替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

答案 2 :(得分:0)

> library(plyr)
> count(x,'Date')

        Date freq
1 01-01-2016    3
2 02-01-2016    2
3 03-01-2016    1