在R中获得成对总和的最快方法是什么?我读到了关于combn但不确定如何在这里应用它的方法。
df <- data.frame(dept= c('MENS APPAREL','MOVIES','BEAUTY','HOME','MOVIES','ELECTRONICS'), GMV=c(78.94,23.84,7.6,15, 2,22.7),order_num=c(1,1,1,2,2,2))
我有什么:
dept TOT_GMV order_num
MENS APPAREL 78.94 1
MOVIES 23.84 1
BEAUTY 7.6 1
HOME 15 2
MOVIES 2 2
ELECTRONICS 22.7 2
我需要什么:
dept pair GMV order_num
MENS APPAREL AND BEAUTY 86.54 1
MENS APPAREL AND MOVIES 102.78 1
MOVIES & BEAUTY 31.44 1
HOME AND MOVIES 17 2
HOME AND ELECTRONICS 37.7 2
MOVIES AND ELECTRONICS 24.7 2
谢谢!
答案 0 :(得分:1)
我找到了解决方案。如果有更短的方法可以告诉我。
df <- data.frame(dept= c('MENS APPAREL','MOVIES','BEAUTY','HOME','MOVIES','ELECTRONICS'), GMV=c(78.94,23.84,7.6,15, 2,22.7),order_num=c(1,1,1,2,2,2))
df1 <- df
dd <- merge(df,df1,by=c("order_num"),all.y=TRUE)
dd1 <- subset(dd,dept.x != dept.y)
dd2 <- dd1[!duplicated(t(apply(dd1, 1, sort))), ]
dd2$GMV <- dd2$GMV.x + dd2$GMV.y
dd2[,c("order_num","dept.x","dept.y","GMV")]
order_num dept.x dept.y GMV
1 MENS APPAREL MOVIES 102.78
1 MENS APPAREL BEAUTY 86.54
1 MOVIES BEAUTY 31.44
2 HOME MOVIES 17.00
2 HOME ELECTRONICS 37.70
2 MOVIES ELECTRONICS 24.70
答案 1 :(得分:1)
虽然以下内容未返回您正在查找的确切输出,但它确实包含所有可能的组合。使用'psych'软件包,您可以使用'%+%'在类似于矩阵乘法的操作中添加两个向量或矩阵:
m <- as.matrix(df[,2])
t <- m%+%t(m)
rownames(t)<- df$dept
colnames(t)<-df$dept
t