0
我想要使用聚合將一些操作應用於一組矩陣,按customer_id
(這是我的數據框的一列df
)分組。在返回矩陣時在R中使用聚合
例如,我想要取對應於不同customer_id
的df
的子集,並向這些子集中添加一些列,然後全部返回。
在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)
}