在R中,如何计算每个Deal_id的CustomerId数量?

时间:2015-02-02 21:36:53

标签: r analytics

我有专栏:Deal_id,Customer_id

每个交易ID的多个客户ID。 我想添加一个列,其中包含每个交易ID的唯一客户数。我们怎么做?

1 个答案:

答案 0 :(得分:1)

对这个问题有点困惑,但这对你有用。

File = structure(list(DealID = c(99L, 99L, 99L, 99L, 11L, 11L, 11L, 
14L, 14L, 15L, 15L), CustomerID = c(1L, 3L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L)), .Names = c("DealID", "CustomerID"), class = "data.frame", row.names = c(NA, 
-11L))

sqldf("Select DealID,count(*) as Total from File group by DealID")
File = unique(File)
sqldf("Select DealID,count(*) as Unique_Total from File group by DealID")