Sum previous observations with parameters

Hello.
I think you can definitely help me

I have a very important question. I wanna sum data with previous one times a parameter (1/2)^i
Here is an exemple and i insert a picture to explain.

A B
2 2
4 5 i.e (4+1/22)
6 8.5 i.e [6+ 1/2 4 + (1/2)^2 2]
8 12.25 i.e [8+ 1/2
6 + (1/2)^2 *4 + (1/2)^3 *2]

And so on.

I have 366 observations in the columns.

Thank you in advance.

Merci encore

suppressPackageStartupMessages(library(dplyr))
mtcars %>% select(mpg) %>% mutate(lagged = mpg * (0.5*row_number() * lag(mpg)))
#>     mpg    lagged
#> 1  21.0        NA
#> 2  21.0   441.000
#> 3  22.8   718.200
#> 4  21.4   975.840
#> 5  18.7  1000.450
#> 6  18.1  1015.410
#> 7  14.3   905.905
#> 8  24.4  1395.680
#> 9  22.8  2503.440
#> 10 19.2  2188.800
#> 11 17.8  1879.680
#> 12 16.4  1751.520
#> 13 17.3  1844.180
#> 14 15.2  1840.720
#> 15 10.4  1185.600
#> 16 10.4   865.280
#> 17 14.7  1299.480
#> 18 32.4  4286.520
#> 19 30.4  9357.120
#> 20 33.9 10305.600
#> 21 21.5  7652.925
#> 22 15.5  3665.750
#> 23 15.2  2709.400
#> 24 13.3  2425.920
#> 25 19.2  3192.000
#> 26 27.3  6814.080
#> 27 26.0  9582.300
#> 28 30.4 11065.600
#> 29 15.8  6964.640
#> 30 19.7  4668.900
#> 31 15.0  4580.250
#> 32 21.4  5136.000

Created on 2020-07-24 by the reprex package (v0.3.0)

1 Like

Hello.
I thank you for your answer. It's very near to what i wanted. But the operation is not what I meant.

Let us consider your exemple, Every "lagged" value must be exactly the mpg + the half of the previous "lagged".

The first lagged should be 21
The second one should be 21 added to half of the first 21 = 31.5
The third one should be 22.8 added to half of 31.5 = 36.75

The fourth one should be 21.4 added to half of 36.75 = 39.775
And so on

THank you in advance

So, how would you change

lagged = mpg * (0.5*row_number() * lag(mpg))

the thing is,
on each observation should be added ALL the past ones times (0.5) with the row range as exponent.

this is the mathematical expression

How would you combine sum, lag and row_number functions to add the value of the first row of column X through the current row of column Y?

Right, the first row X=Y
Because there is no lag and row_number=1
Y_1= X_1
Y_2=X_2+ (1/2)^1X_1
Y_3=X_3 + (1/2)^1
X_2 +(1/2)^2 * X_1

And so on
In each case it ends up with X_1

And also Y_4=X_4 + (1/2)^1 X_3 + (1/2)^2 X_2 + (1/2)^3 * X_1

Another way we can do it,
Maybe easier:

Y_1 =X_1
Y_2= X_2 + (1/2)Y_1
Y_3= X_3+ (1/2)
Y_2
Y_4=X_4+ (1/2)* Y_3
Y_5= X_5+ (1/2)* Y_4

X_2 is not based on any previous value, but is given by the user?

We already have all the X_i
We just need the Y_i

Personally, I have data X_1 to X_366
And I want to obtain the Y_1 to Y_366
Where Y_1=X_1

Thanks for clarifying

As I told you, I have 366 observations

Y_1 =X_1
Y_2= X_2 + (1/2)Y_1
Y_3= X_3+ (1/2)
Y_2
Y_4=X_4+ (1/2)* Y_3
Y_5= X_5+ (1/2)* Y_4
. .
. .
. .
. .
. .
. .
Y_366=X_366+ (1/2)*Y_365

options(scipen=999)

sum_nextx <- function(xvec,i){

  results <- vector(length=length(xvec))
  y<-0
  for (index in  seq_along(results)) {
    y <-  xvec[index] + y * (1/2) ^ i
    results[index] <- y
    cat(index, "\t", round(y,5),"\n")
  }
  results
}

r<- sum_nextx((1:10),.5)

Good Morning Nirgrahamuk

sorry, it does not work.
where do I have to put my vector of data? my data is DF.NS$Lunenburg
it does not work

can you please explain a little?

@nirgrahamuk

I already have a column of data (Lunenburg) in the dataframe (DF):

for each observation in Lunenburg (X_i) I would like to get the response (Y_i) as defined below.

I tried your code but it does not work.