The zoo package is esp designed for such tasks.
library(zoo)
df1$new <- unlist(tapply(df1$value, factor(df1$name), function(x){ zoo::rollsum(x, 2, align = "left", fill = x[length(x)]) }))
#> df1$new
#[1] 5 6 3 7 5 3 4 3
df1 <- data.frame(stringsAsFactors=FALSE,
                  name = c("a", "a", "a", "b", "b", "b", "b", "b","c","d","d","d"),
                  value = c(2L, 3L, 3L, 4L, 3L, 2L, 1L, 3L, 4L, 1L:3L)
)
windowSize = 3
df1$new <- unlist(
    tapply(df1$value, factor(df1$name),function(x){
        IND <- (length(x)-(windowSize-2)):length(x);IND = IND[IND > 0]
        c(  zoo::rollsum(x, windowSize, align = "left"), rev(cumsum(rev(x[IND])))  )})
    )
This was a little bit tricky to do:
Here is the formula in respect to a given windowSize.