Here's another solution if you want to stick with sqldf
:
library(sqldf)
sqldf("select itemID1, itemID2, sum(value) as value
from (select case when itemID1 <= itemID2 then itemID1 else itemID2 end as itemID1,
case when itemID1 > itemID2 then itemID1 else itemID2 end as itemID2,
value
from df)
group by itemID1, itemID2")
Result:
itemID1 itemID2 value
1 B0001 B0001 1
2 B0001 B0002 3
3 B0002 B0002 0
Data:
df = structure(list(itemID1 = structure(c(1L, 2L, 1L, 2L), .Label = c("B0001",
"B0002"), class = "factor"), itemID2 = structure(c(1L, 1L, 2L,
2L), .Label = c("B0001", "B0002"), class = "factor"), value = c(1L,
1L, 2L, 0L)), .Names = c("itemID1", "itemID2", "value"), class = "data.frame", row.names = c(NA,
-4L))
source
share