2016-06-27 24 views
0

私は同様の質問をしているのを見たことがありますが、自分のデータには適用できません。私は、Value、Value 2、Value 3をProduct IDとRevenueで集計しようとしていますが、Valueの集計は合計です。しかしながら;私は唯一の重複ここ rで合計と重複を集計

に1つの値を表示するには値2および値3をしたいことは、私のコードです:

aggregate(Value, Value2, Value3 ~product_id + Revenue, dat,sum) 

データ:

product i_d Revenue Value Value2 Value 3 
1   in  1  1  1 
2   in  6  2  2 
1   out  0  3  3 
2   out  0  4  4 
:それは次のようになりますように

dat <-structure(list(product_id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), 
       Date = c("January", "February", "March", "January", "February", "March", "January", "February", "March", "January", "February", "March"), 
       Revenue = c("in", "in", "in", "out", "out", "out", "in", "in", "in", "out", "out", "out"), 
       Value = c(0L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 3L, 0L, 0L, 0L), 
      Value2 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), 
      Value3 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L) 
      ), 
      .Names = c("product_id", "Date", "Revenue", "Value", "Value2", "Value3"), 
      class = "data.frame", row.names = c(NA, -12L)) 

+0

データ定義にタイプミスがあります。 Value2は持っていませんが、Value3は2回定義されています。 – lmo

+0

これを修正すると、 'aggregate(cbind(Value、Value2、Value3)〜product_id + Revenue、data = dat、sum)'が動作します。 'cbind'を使うと、計算する複数の変数を含めることができます。 – lmo

答えて

0
dat <-structure(list(product_id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), 
        Date = c("January", "February", "March", "January", "February", "March", "January", "February", "March", "January", "February", "March"), 
        Revenue = c("in", "in", "in", "out", "out", "out", "in", "in", "in", "out", "out", "out"), 
        Value = c(0L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 3L, 0L, 0L, 0L), 
        Value2 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), 
        Value3 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L) 
), 
.Names = c("product_id", "Date", "Revenue", "Value", "Value2", "Value3"), 
class = "data.frame", row.names = c(NA, -12L)) 

res <- aggregate(dat[,colnames(dat) %in% c("Value", "Value2", "Value3")],by=list(dat$product_id, dat$Revenue),FUN=sum) 

colnames(res) <- c("product_id", "Revenue", "Value", "Value 2", "Value 3") 
res 
    product_id Revenue Value Value 2 Value 3 
1   1  in  1  3  3 
2   2  in  6  9  9 
3   1  out  0  6  6 
4   2  out  0  12  12 
関連する問題