R中每个客户的日期计数
对于以下数据R中每个客户的日期计数,r,R,对于以下数据 custid <- c(1,1,1,2,2,5,6,8,8,8) date <- c(20130401, 20130403, 20130504, 20130508, 20130511, 20130716, 20130719, 20130423, 20130729, 20130707) money <- c(1, 2, 45, 3.56, 32.39, 1, 2, 3.90, 4, 8.5) file <- d
custid <- c(1,1,1,2,2,5,6,8,8,8)
date <- c(20130401, 20130403, 20130504, 20130508, 20130511, 20130716,
20130719, 20130423, 20130729, 20130707)
money <- c(1, 2, 45, 3.56, 32.39, 1, 2, 3.90, 4, 8.5)
file <- data.frame(custid, date, money)
你可以试试
aggregate(cbind(Trans_count=date)~custid, file, length)
或
或
或者如果您需要向量输出
table(file$custid)
library(data.table)
setDT(file)[, list(Trans_count=.N), .(custid)]
library(dplyr)
file %>%
group_by(custid) %>%
summarise(Trans_count=n())
table(file$custid)