2015-02-23 24 views
-1

我正在計算每個客戶ID的平均值或平均值。對於下面的數據:R中每個客戶的平均成本

customerID <- c(1,1,1,1,2,2,2,2,3,3) 

dates <- c(20130401, 20130403, 20130504, 20130508, 20130511, 
     20130716, 20130719, 20130723, 20130729, 20130907) 
cost <- c(12, 41, 89, 45.5, 32.89, 74, 76, 12, 15.78, 10) 

data <- data.frame(customerID, dates,cost) 

data$dates <- as.Date(as.character(data$dates), "%Y%m%d") 

# data2 <- aggregate(cbind(average_cost=cost) + customerID, data, mean) 

的數據是這樣的:

customerID dates cost 
1 20130401 12 
1 20130403 41 
1 20130504 89 
1 20130508 45.5 
2 20130511 32.89 
2 20130716 74 
2 20130719 76 
2 20130723 12 
3 20130729 15.78 
3 20130907 10 

我怎樣才能得到類似這樣的輸出?我可以得到整個數據集的平均值,但不是每個客戶的ID。謝謝!

customerID average_cost 
1   46.875 
2   48.7225 
3   12.89 
+0

@大衛arenburg。是的,它是不同的。以前,它是爲每個客戶查找總數以及日期。這個問題只涉及每個客戶的客戶和平均水平。日期在這裏並不重要,就像他們對前面的問題所做的一樣。數據集值似乎相似,但我嘗試了不同的場景。 – sharp 2015-02-23 14:59:56

+0

這個問題是dupe的一個特例。 – 2015-02-23 15:00:59

+0

@DavidArenburg。原來如此。沒有看到頂部的鏈接。謝謝。 – sharp 2015-02-23 15:04:17

回答

0

dplyr解決方案

library(dplyr) 
df %>% 
    group_by(customerID) %>% 
    summarise(average_cost = mean(cost)) 

    customerID average_cost 
1   1  46.8750 
2   2  48.7225 
3   3  12.8900 

data.table解決方案

library(data.table) 
dt <- as.data.table(df) 
dt[, .(average_cost = mean(cost)), by=customerID] 

此外,如果你只是想基礎R

aggregate(cost ~ customerID, data=df, mean) 
+0

謝謝!我用聚合(成本〜customerID,數據= df,平均數) – sharp 2015-02-23 14:56:03