2015-03-02 48 views
-1

對於下面的數據,日期數中的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 <- data.frame(custid, date, money) 

我如何獲得這個輸出?我通過計算日期來獲取Trans_count值。謝謝。

custid Trans_count 
1  3 
2  2 
5  1 
6  1 
8  3 

回答

0

你可以嘗試要麼

aggregate(cbind(Trans_count=date)~custid, file, length) 

或者

library(data.table) 
setDT(file)[, list(Trans_count=.N), .(custid)] 

或者

library(dplyr) 
file %>% 
    group_by(custid) %>% 
    summarise(Trans_count=n()) 

,或者您需要一個矢量輸出

table(file$custid)