Search code examples
rdplyrweighted

calculating specific kind of weighted mean in R based on specific sequence


I have some data as follows, which I want to have a specific kind of weighted mean for one column.

    library("tidyverse")
    Mydata<-
    tibble(
    id = c(1,2,3,4,5,6,7,8,9,10),
    AGE=  c(15,16,17, 18, 19, 20, 21, 22, 23,24),
    Prob= c(25,30,43,43,25,25,25,25,67   
                 ,33)) 

I want something like the ensuing function to happen for all of the prob columns from above to, bottom :

    prob.age15=(X+ (X+1)*2+ (X+2)*2+(X+3)*2+ (X+4))/8

    prob.age16=((X+1)+ (X+2)*2+(X+3)*2+ (X+4)*2+(X+5))/8
    porb.age17...

In the end, I want to have something like this:

enter image description here


Solution

  • I think you are looking for lead:

    Mydata %>%
      mutate(result = (Prob + 2 * lead(Prob) + 2 * lead(Prob, 2) + 
                         2 * lead(Prob, 3) + lead(Prob, 4))/8) %>%
      as.data.frame()
    #>    id AGE Prob result
    #> 1   1  15   25 35.250
    #> 2   2  16   30 34.625
    #> 3   3  17   43 31.750
    #> 4   4  18   43 27.250
    #> 5   5  19   25 30.250
    #> 6   6  20   25 36.500
    #> 7   7  21   25     NA
    #> 8   8  22   25     NA
    #> 9   9  23   67     NA
    #> 10 10  24   33     NA