I've tried using the quantile function but it doesn't do as intended. 

I also used new function

```
 percentile <- function(Df, percentile = 50)
   {
   Df_Names <- names(Df)
  percentile <- percentile/100
  f <- function(x, p) c(NA, x[-length(x)]) + p * c(NA, diff(x))
  while(length(which(!is.na(Df[[length(Df)]]))) > 1)          
  {
Df <- cbind(Df, f(Df[[length(Df)]], percentile))
}
setNames(Df, c(Df_Names, paste0("v", 1:(length(df) - length(df_Names)))))
}

Output=data.frame(pecentile(df, percentile = 50))

```

Assume we are finding the P50 where I mention percentiles. Note: I used excel to calculate the percentiles with the percentile function, but the results should be the same using R. In most instances there are thousands of data points per column and R would be more time efficient to calculate them.

an example data set would be 

```
  Date           data
  2020-01-01      49.5
  2020-02-01      49.7
  2020-03-14      48.5
  2020-05-02      50.2

```

I'm trying to have the script result in a percentile output using the column before it as a starting point. However columns after V1 their percentiles are calculated differently than V1. As shown here 

V1 Calculation

[![V1 calculation](https://i.stack.imgur.com/4BxMm.png)](https://i.stack.imgur.com/4BxMm.png)

Where 49.35 is the percentile calculation in excel using 48.5 and 50.2 in the red box and 49.1 is the result of using 49.7 and 48.5 in the blue box that overlaps the red box.

and V2 calculation 

[![V2 calculation](https://i.stack.imgur.com/ItX05.png)](https://i.stack.imgur.com/ItX05.png)

where 49.225 is the result of using 49.1 and 49.35 in the red box while 49.35 is the result of using 49.6, 49.1 and 49.35 in the blue box that overlaps the red box.

V2 calculation starts off the same, but the calculations above it include the adjacent cell plus the cells below the adjacent cells, which the script doesn't do.

The result from this script should be: 

```
  Date           data    V1     V2      V3
  2020-01-01      49.5   49.6   49.35    49.2875
  2020-02-01      49.7   49.1   49.225
  2020-03-14      48.5   49.35
  2020-05-02      50.2

```

Note2: The script purposes is to be in a loop until the script creates the final column in this instance the final column is V3. but in reality it may go up to V800 or more

Then when this is done bring down the results without altering the output, resulting in:

```
  Date           data    V1     V2      V3
  2020-01-01      49.5   NA     NA      NA
  2020-02-01      49.7   49.6   NA      NA
  2020-03-14      48.5   49.1   49.35   NA
  2020-05-02      50.2   49.35  49.225  49.2875

```

NOTE: The function works well to calculate the P50 in V1 which is fine, but it drops the ball with V2, V3 etc.