rtable-functions

how to calculate the lift value of table in R?`


library(readr)
data1<-read_csv(".../file1")
data2<-read_csv(".../file2")
table2<-table(data1$`_SEGMENT_`,data2$`_SEGMENT_`,data1$Subscribed)

This is the frequency table2 I created with three variables, data1 segment as row, data2 segment as column and third dimension 'Subscribed' with 'yes' and 'no'.

> table2
, ,  = no

   
       1    2    3    4    5
  1   29  674 5189 7207   88
  2    3  194 1393 2166   39
  3   18  471 2667 5719   77
  4    0    1    2   11    0
  5   18  420 2798 4715   88

, ,  = yes

   
       1    2    3    4    5
  1    0   33  262 1000    1
  2    0   10   65  322    0
  3    0   25  190 1206    2
  4    0    0    1    3    0
  5    0   26  166  943    3

How to create a lift value table of 'yes' based on table2? 5x5 table and each position value = value of 'yes'/(value of 'yes'+value of 'no')

e.g. table_liftvalue[1,2] = 33/(33+674)

> table_liftvalue

       1    2    3    4    5
  1   . 33/(674+33)
  2   .
  3   .
  4   .
  5   .

I appreciate any help. [1]: https://i.sstatic.net/1w6mj.png


Solution

  • Using apply for the sum, then just divide.

    table2[,,"yes"]/(apply(table2, 1:2, sum))
    #      [,1]       [,2]       [,3]      [,4]       [,5]
    # [1,]    0 0.04667610 0.04806458 0.1218472 0.01123596
    # [2,]    0 0.04901961 0.04458162 0.1294212 0.00000000
    # [3,]    0 0.05040323 0.06650333 0.1741516 0.02531646
    # [4,]  NaN 0.00000000 0.33333333 0.2142857        NaN
    # [5,]    0 0.05829596 0.05600540 0.1666667 0.03296703
    

    Data:

    table2 <- structure(c(29L, 3L, 18L, 0L, 18L, 674L, 194L, 471L, 1L, 420L, 
    5189L, 1393L, 2667L, 2L, 2798L, 7207L, 2166L, 5719L, 11L, 4715L, 
    88L, 39L, 77L, 0L, 88L, 0L, 0L, 0L, 0L, 0L, 33L, 10L, 25L, 0L, 
    26L, 262L, 65L, 190L, 1L, 166L, 1000L, 322L, 1206L, 3L, 943L, 
    1L, 0L, 2L, 0L, 3L), .Dim = c(5L, 5L, 2L), .Dimnames = list(NULL, 
        NULL, c("no", "yes")))