简体   繁体   English

使用 data.table 计算广告资源

[英]Calculate adstock using data.table

I have a dataframe as shown:我有一个数据框,如图所示:

structure(list(ID = c(1, 1, 1, 1, 2, 2, 2, 2), ColA = c(2, 3, 
4, 5, 2, 3, 4, 5), ColB = c(1, 2, 3, 4, 1, 2, 3, 4), ColA_0.2 = c(2, 
3.4, 4.68, 5.936, 2, 3.4, 4.68, 5.936), ColB_0.2 = c(1, 2.2, 
3.44, 4.688, 1, 2.2, 3.44, 4.688)), class = "data.frame", row.names = c(NA, 
-8L))

What I need ?我需要的 ? - For each ID, I want to calculate ColA_ad and ColB_ad . - 对于每个 ID,我想计算ColA_adColB_ad User will pass a parameter 'ad'.用户将传递参数“ad”。

For example - if 'ad' is 0.2 then the values will be calculated as:例如 - 如果 'ad' 为 0.2,那么这些值将计算为:

  • First row - same as ColA (ie 2)第一行 - 与ColA相同(即 2)
  • Second row - Add Second row of ColA to 0.2*First row of ColA_ad (ie Sum(3,0.2*2)=3.4 )第二排-添加的第二排ColA为0.2 *第一行ColA_ad (即Sum(3,0.2*2)=3.4
  • Third row - Add Third row of ColA to 0.2*second row of ColA_ad (ie Sum(4,0.2*3.4)=4.68 ) and so on.第三排-添加第三排ColA为0.2 *第二排ColA_ad (即Sum(4,0.2*3.4)=4.68 )等。

The same will be calculated for all other columns (here ColB), which can be mentioned in separate vector.对于所有其他列(此处为 ColB)计算相同,可以在单独的向量中提及。

Summary - I would take 0.2 times carry over effect of previous calculated row and add to new row.总结 - 我会采用前一个计算行的 0.2 倍结转效果并添加到新行。

The results are displayed in Column ColA_ad and ColB_ad .结果显示在列ColA_adColB_ad

As my dataset is very large, I am looking for data.table solution.由于我的数据集非常大,我正在寻找 data.table 解决方案。

Here is a base R solution, where a linear algebra property is applied to speed up your iterative calculation.这是一个基本的 R 解决方案,其中应用了线性代数属性来加速迭代计算。

  • basic idea (taking id = 1 as example)基本思路(以id = 1为例)

    • you first construct a low triangluar matrix for mapping from col to col_ad , ie,您首先构造一个低三角矩阵,用于从col映射到col_ad ,即,
l <- 0.2**abs(outer(seq(4),seq(4),"-"))
l[upper.tri(l)] <- 0

which gives这使

> l
      [,1] [,2] [,3] [,4]
[1,] 1.000 0.00  0.0    0
[2,] 0.200 1.00  0.0    0
[3,] 0.040 0.20  1.0    0
[4,] 0.008 0.04  0.2    1
  • then you use l over columns col , ie,然后你在列col使用l ,即,
> l %*% as.matrix(subset(df,ID == 1)[-1])
      ColA  ColB
[1,] 2.000 1.000
[2,] 3.400 2.200
[3,] 4.680 3.440
[4,] 5.936 4.688
  • code代码
ad <- 0.2
col_ad <- do.call(rbind,
                  c(make.row.names = F,
                    lapply(split(df,df$ID), 
                           function(x) {
                             l <- ad**abs(outer(seq(nrow(x)),seq(nrow(x)),"-"))
                             l[upper.tri(l)]<- 0
                             `colnames<-`(data.frame(l%*% as.matrix(x[-1])),paste0(names(x[-1]),"_",ad))
                           }
                    )
                  )
)

dfout <- cbind(df,col_ad)

such that以至于

> dfout
  ID ColA ColB ColA_0.2 ColB_0.2
1  1    2    1    2.000    1.000
2  1    3    2    3.400    2.200
3  1    4    3    4.680    3.440
4  1    5    4    5.936    4.688
5  2    2    1    2.000    1.000
6  2    3    2    3.400    2.200
7  2    4    3    4.680    3.440
8  2    5    4    5.936    4.688
  • DATA数据
df <- structure(list(ID = c(1, 1, 1, 1, 2, 2, 2, 2), ColA = c(2, 3, 
                                                              4, 5, 2, 3, 4, 5), ColB = c(1, 2, 3, 4, 1, 2, 3, 4)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                        -8L))

A non-recursive option:非递归选项:

setDT(DT)[, paste0(cols,"_",ad) := { 
    m <- matrix(unlist(shift(ad^(seq_len(.N)-1L), 0L:(.N-1L), fill = 0)), nrow=.N)
    lapply(.SD, function(x) c(m%*%x))
}, by = ID, .SDcols = cols]

Another recursive option:另一个递归选项:

library(data.table)
setDT(DT)[, paste0(cols,"_",ad) := {
        a <- 0
        b <- 0
        .SD[, {
            a <- ColA + ad*a        
            b <- ColB + ad*b
            .(a, b)

        }, seq_len(.N)][, (1) := NULL]
    }, 
    by = ID]

output:输出:

   ID ColA ColB ColA_0.2 ColB_0.2
1:  1    2    1    2.000    1.000
2:  1    3    2    3.400    2.200
3:  1    4    3    4.680    3.440
4:  1    5    4    5.936    4.688
5:  2    2    1    2.000    1.000
6:  2    3    2    3.400    2.200
7:  2    4    3    4.680    3.440
8:  2    5    4    5.936    4.688

data:数据:

DT <- structure(list(ID = c(1, 1, 1, 1, 2, 2, 2, 2), ColA = c(2, 3, 
    4, 5, 2, 3, 4, 5), ColB = c(1, 2, 3, 4, 1, 2, 3, 4), ColA_0.2 = c(2, 
        3.4, 4.68, 5.936, 2, 3.4, 4.68, 5.936), ColB_0.2 = c(1, 2.2, 
            3.44, 4.688, 1, 2.2, 3.44, 4.688)), class = "data.frame", row.names = c(NA, 
                -8L))
ad <- 0.2
cols <- c("ColA", "ColB")

Here is one way with data.table using Reduce :这是使用Reduce data.table一种方法:

#Columns to apply function to
cols <- names(df)[2:3]

#Create a function to apply 
apply_fun <- function(col, ad) {
   Reduce(function(x, y) sum(y, x * ad), col, accumulate = TRUE)
}

library(data.table)
#Convert dataframe to data.table
setDT(df)
#set ad value
ad <- 0.2
#Apply funnction to each columns of cols
df[, (paste(cols, ad, sep =  "_")) := lapply(.SD, apply_fun, ad), .SDcols = cols, by = ID]

df
#   ID ColA ColB ColA_0.2 ColB_0.2
#1:  1    2    1    2.000    1.000
#2:  1    3    2    3.400    2.200
#3:  1    4    3    4.680    3.440
#4:  1    5    4    5.936    4.688
#5:  2    2    1    2.000    1.000
#6:  2    3    2    3.400    2.200
#7:  2    4    3    4.680    3.440
#8:  2    5    4    5.936    4.688

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM