Aggregation Regardless of Column Order

I want to aggregate a block of data across two columns, so changing them only exists once. The column of values ​​must be aggregated using an aggregation function such as max()orsum()

Data:

itemID1  |itemID2  |value
---------|---------|-------
B0001    |B0001    |1
B0002    |B0001    |1
B0001    |B0002    |2
B0002    |B0002    |0

The result may be:

itemID1   |itemID2   |value
----------|----------|---------
B0001     |B0001     |1
B0001     |B0002     |3          #itemIDs could also be ordered in the other way
B0002     |B0002     |0

So far I have implemented it in SQL to use it through the sqldf library, but sqldf does not support WITH-clauses.

Is there any way to aggregate data like this directly in R?

+4
source share
4 answers

with dplyrand pmin/ pmax:

library(dplyr)
df1 %>%
  mutate(ItemID1_ = pmin(itemID1  ,itemID2),
         ItemID2_ = pmax(itemID1  ,itemID2)) %>%
  group_by(ItemID1_,ItemID2_) %>%
  summarize_at("value",sum) %>%
  ungroup

# # A tibble: 3 x 3
#   ItemID1_ ItemID2_ value
#      <chr>    <chr> <int>
# 1    B0001    B0001     1
# 2    B0001    B0002     3
# 3    B0002    B0002     0

Following the comment by @ A5C1D2H2I1M1N2O1R2T1, you can skip part of the mutant and get the same result:

df1 %>%
  group_by(itemID1_ = pmin(itemID1, itemID2),
           itemID2_ = pmax(itemID1, itemID2)) %>%
  summarise_at("value", sum) %>%
  ungroup
+4
source

base R, , , .

dat2 <- dat
dat2[1:2] <- apply(dat2[1:2], 1, sort)
aggregate(value ~ itemID1 + itemID2, dat2, sum)
#  itemID1 itemID2 value
#1   B0001   B0001     1
#2   B0001   B0002     3
#3   B0002   B0002     0

rm(dat2), .

DATA.

dat <-
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))
+6

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))
+4
source

For completeness, there is also a solution data.table:

library(data.table)
setDT(DT)[, .(value = sum(value)), 
   by = .(itemID1 = pmin(itemID1, itemID2), itemID2 = pmax(itemID1, itemID2))]
   itemID1 itemID2 value
1:   B0001   B0001     1
2:   B0001   B0002     3
3:   B0002   B0002     0

Data

DT <- fread("itemID1  |itemID2  |value
B0001    |B0001    |1
B0002    |B0001    |1
B0001    |B0002    |2
B0002    |B0002    |0", sep = "|")
+3
source

Source: https://habr.com/ru/post/1688169/


All Articles