如何在R中聚合无向组合

时间:2016-06-20 02:36:50

标签: r aggregate combinations

我有一个3列的数据框

A B 1
A B 1
A C 1
B A 1

我想汇总它,以便它认为组合A-B和B-A是相同的,导致

A B 3
A C 1

我该怎么做?

1 个答案:

答案 0 :(得分:1)

在前两列上使用pminpmax,然后逐个计数:

library(dplyr);
df %>% group_by(G1 = pmin(V1, V2), G2 = pmax(V1, V2)) %>% summarise(Count = sum(V3))
Source: local data frame [2 x 3]
Groups: G1 [?]

     G1    G2 Count
  (chr) (chr) (int)
1     A     B     3
2     A     C     1

相应的data.table解决方案是:

library(data.table)
setDT(df)
df[, .(Count = sum(V3)), .(G1 = pmin(V1, V2), G2 = pmax(V1, V2))]

   G1 G2 Count
1:  A  B     3
2:  A  C     1

数据

structure(list(V1 = c("A", "A", "A", "B"), V2 = c("B", "B", "C", 
"A"), V3 = c(1L, 1L, 1L, 1L)), .Names = c("V1", "V2", "V3"), row.names = c(NA, 
-4L), class = "data.frame")