r mapply over two Lists of 6?

74 Views Asked by At

I have two Lists of 6.

the first holds the counts of observations of TEST vs. CONTROL, the second holds the SUM of these obseration

I used lapply on both (I am learning) :- )

now I would like to loop through these to do the final calculation.

This is a 2 fold questions:

a) I would like to learn mapply b) if there is a better way to combine what I did, please share

COUNT_T_C   <- lapply(dfList,
                         function(df) 
                         {
                           df %>%
                             group_by(TC_INDICATOR) %>%
                             summarise(CNTCONTROL = length(which(TC_INDICATOR=="CONTROL"))  ,
                                       CNTTEST    = length(which(TC_INDICATOR=="TEST")) )
                         })


# Sum Control over Test in all 6 lists
SUM_T_C      <- lapply(dfList,
                       function(df) 
                       {
                         df %>%
                           group_by(TC_INDICATOR) %>%
                           summarise(SUMCONTROL = sum(NET_SPEND[which(TC_INDICATOR=="CONTROL")]),
                                     SUMTEST    = sum(NET_SPEND[which(TC_INDICATOR=="TEST")]) )
                       })

I want to calculate Incremental sales, the formula for that is:

For all 6 lists.

 INCREMENTAL_SALES <- (SUMTEST / CNTTEST) - (SUMCONTROL / CNTTEST)

Here is some sample output and expected values back for the Inc Sales:

> COUNT_T_C[1:3]
$Spend_201606
  CNTCONTROL CNTTEST
1      12749   50928

$Spend_201607
  CNTCONTROL CNTTEST
1      19646   79126

$Spend_201608
  CNTCONTROL CNTTEST
1      12231   49116



> SUM_T_C[1:3]
$Spend_201606
  SUMCONTROL SUMTEST
1    1398013 6004852

$Spend_201607
  SUMCONTROL  SUMTEST
1    3555418 15136397

$Spend_201608
  SUMCONTROL SUMTEST
1    1823619 7632856


for the first observation from each Set it would work like this:

obs1 <- (6004852 / 50928) - (1398013 / 12749)     

8.25
0

There are 0 best solutions below