0

I have a SKU -week data which has different in-store activities (Tactics). I want to summarize each variables by the tactics. Pasting the code in SAS:

proc sql; 
create table lp.lp_sku_report1 
as select distinct(tactic), sum(Sales_Stat_Case_10_Lt) as Sales_Stat_Case_10_Lt, sum(Sales_Units) as Sales_Units, sum(Sales_Dollars) as Sales_Dollars,  sum(Baseline_Stat_Case_10_Lt) as Baseline_Stat_Case_10_Lt, sum(Baseline_Units) as Baseline_Units, sum(Baseline_Dollars) as Baseline_Dollars
from lp.lp_sku_data 
group by tactic; quit;

1 Answers1

2

with dplyr package it's very easy to perform this action. first group by the variable tactic and then summarise rest of the variables by using the aggregating function sum.

In this specific case since the aggregating function is same for all the variables you can use summarise_each to apply the same function to all variables.

below is the code

library(dplyr)

    df = df %>%
      group_by(tactic) %>%
      summarise_each(funs(sum))
  • Better add some explanatory text or it will be flagged for possible deletion as "too short". SO is looking for quality... – Mike Wise Dec 21 '15 at 06:44