简体   繁体   中英

function in R that creates a new column in a data.table, whose i-th row is the sum of product of two columns starting from i+1 row

Example:

data.table(x=1:3, y=4:6)

I want to insert a new column whose values would be:

z=(2*5+3*6, 3*6, NA)

I tried to create this function firstly but it doesnt work:

sumprod <- function(x, y){
  z=vector()
  for (i in 1:length(x)-1){
    z=c(z, sum(shift(x, n=i+1, type="lag")*shift(y, n=i+1, type="lag"), na.rm=FALSE))
  }
  return(z)
}

We may do

library(data.table)
dt1[, z := rev(cumsum(rev(Reduce(`*`, shift(.SD, type = "lead",
    fill = 0)))))]
dt1[z == 0, z := NA_real_]

-output

> dt1
   x y  z
1: 1 4 28
2: 2 5 18
3: 3 6 NA

Or with fcumsum

library(collapse)
dt1[, z := fcumsum(shift(do.call(`*`, .SD), type = "lead")[.N:1])[.N:1]]

data

dt1 <- data.table(x=1:3, y=4:6)

You could Reduce using right=T argument (from right to left):

dt[,z:=shift(Reduce('+',x*y,accumulate=T,right=T),-1)][]

       x     y     z
   <int> <int> <int>
1:     1     4    28
2:     2     5    18
3:     3     6    NA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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