r - Sum values of combinations within a group -


for analysis transform data from:

data <- data.frame(   customer = c("a", "a", "b", "b", "c", "c", "c"),   product = c("x", "y", "x", "z", "x", "y", "z"),   value = c(10, 15, 5, 10, 20, 5, 10) ) data #   customer product value # 1              x    10 # 2              y    15 # 3        b       x     5 # 4        b       z    10 # 5        c       x    20 # 6        c       y     5 # 7        c       z    10 

to:

product product sum value -------|-------|--------- x      |y      |50 x      |z      |45 y      |z      |15 

basically want sum of value every product combination within customer. guess work of reshape package cannot work.

thanks time.

here 1 way, in 2 steps:

1) transform data long data.frame of pairs within customers. that, rely on combn provide indices of possible pairs:

process.one <- function(x) {    n <- nrow(x)    <- combn(n, 2)    data.frame(product1 = x$product[i[1, ]],               product2 = x$product[i[2, ]],               value    = x$value[i[1, ]] +                          x$value[i[2, ]]) }  library(plyr) long <- ddply(data, "customer", process.one) long #   customer product1 product2 value # 1               x        y    25 # 2        b        x        z    15 # 3        c        x        y    25 # 4        c        x        z    30 # 5        c        y        z    15 

2) drop customer dimension , aggregate values:

aggregate(value ~ ., long[c("product1", "product2", "value")], sum) #   product1 product2 value # 1        x        y    50 # 2        x        z    45 # 3        y        z    15 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -