2014-11-24 54 views
0

我想要使用聚合將一些操作應用於一組矩陣,按customer_id(這是我的數據框的一列df)分組。在返回矩陣時在R中使用聚合

例如,我想要取對應於不同customer_iddf的子集,並向這些子集中添加一些列,然後全部返回。

在Python中,我會使用groupby並應用。

如何在R中可以做到這一點?

我寫的代碼如下所示:

gr_TILPS = aggregate(df,by=list(df[,"customer_id"]),FUN=kmeansfunction) 

Error in TILPSgroup$hour : $ operator is invalid for atomic vectors 

錯誤從kmeansfunction我想,它看起來像未來:

kmeansfunction = function(dfgroup){ 

Hour =dfgroup$hour 
Weekday =TILPSgroup$WeekdayPrime 
x <- cbind(Hour, Weekday) 
colnames(x) <- c("x", "y") 
(cl <- kmeans(x, 2)) 
clusters = cl$cluster 
origclusters = as.factor(clusters) 
dfgroup = cbind(dfgroup,origclusters) 

return(dfgroup) 

}

回答

2

aggregate適用同樣的功能到多個單個列。如果你想處理列的合奏,那麼使用這個範例:lapply(split(df,group),function);

試試這個:

gr_TILPS <- lapply(split(df, df[,"customer_id"]), 
        FUN=kmeansfunction) 

聽起來像蟒蛇可能有一些相似的實驗包: 'dplyr'。從某種意義上說,aggregate只是塊內的一個面向列的處理策略,而lapply(split,),)策略更適用於當您對由阻塞條件定義的整行數據感興趣時。如果您以後想要將這些結果重新排列在一起,則始終可以使用do.call(rbind, res_from_lapply)