Verwenden pmin
und pmax
auf den ersten beiden Spalten und führen Sie dann die Gruppe-by-count:
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
Entsprechende data.table
Lösung wäre:
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
Daten:
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")
Relevant - h ttp: //stackoverflow.com/questions/35834385/create-unique-identifier-from-the-interchangeable-combination-of-two-variables/35834584 und http://stackoverflow.com/questions/25297812/pair-wise- duplicate-removal-from-dataframe/25298863 und http://stackoverflow.com/questions/25145982/extract-unique-rows-from-a-data-table-with-each-row-unsorted/25151395 – thelatemail