Cumulative multiplication in R with the addition of extra constants

浪尽此生 提交于 2019-12-12 15:15:20

问题


I'm new to R and struggling with the following combination of cumulative multiplication with the addition of extra constants. I'm looking to achieve the following in a dataframe:

Variable_X         Variable_Y           Variable_Z
X1                 Y1                   Y1*X1      = Z1 
X2                 Y2                   (Z1+Y2)*X2 = Z2 
X3                 Y3                   (Z2+Y3)*X3 = Z3
X4                 Y4                   (Z3+Y4)*X4 = Z4

Any help would be greatly appreciated.


回答1:


library(Rcpp)

cppFunction("NumericVector foo_cpp (NumericVector x, NumericVector y) {
  int n = x.size(), i;
  NumericVector z(n);
  double tmp = 0.0;
  for (i = 0; i < n; i++) {
    tmp = (tmp + y[i]) * x[i];
    z[i] = tmp;
    }
  return z;
  }")

set.seed(0)
dat <- data.frame(x = runif(1e+6), y = runif(1e+6))
system.time(z <- foo_cpp(dat$x, dat$y))
#   user  system elapsed 
#  0.016   0.000   0.014 
dat$z <- z



回答2:


I believe a good old fashioned for loop is the best way to solve this.

dat <- data.frame(Variable_X = 1:10, Variable_Y = 11:20)

dat$Variable_Z <- dat$Variable_X[1]*dat$Variable_Y[1]
for(i in seq_len(nrow(dat))[-1]){
  dat$Variable_Z[i] <- (dat$Variable_Z[i - 1] + dat$Variable_Y[i])*dat$Variable_X[i]
}

dat
#   Variable_X Variable_Y Variable_Z
#1           1         11         11
#2           2         12         46
#3           3         13        177
#4           4         14        764
#5           5         15       3895
#6           6         16      23466
#7           7         17     164381
#8           8         18    1315192
#9           9         19   11836899
#10         10         20  118369190



回答3:


Using only base R define an anonymous function to embody the iteration and then run it using Reduce:

transform(DF, Z = Reduce(f = function(prev, i) (prev + Y[i]) * X[i], 
                         x = seq_along(X), init = 0, acc = TRUE)[-1])

giving (for the input in the Note at the end):

  X   Y    Z
1 1 101  101
2 2 102  406
3 3 203 1827
4 4 104 7724

Added

The gsubfn function allows short forms for function definitions and using that we could rewrite the above as:

library(gsubfn)

transform(DF, Z = fn$Reduce(f = prev + i ~ (prev + Y[i]) * X[i], 
                         x = seq_along(X), init = 0, acc = TRUE)[-1])

Note

We used the following input:

DF <- data.frame(X = 1:4, Y = 101:104)


来源:https://stackoverflow.com/questions/52130992/cumulative-multiplication-in-r-with-the-addition-of-extra-constants

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!