Q:

How can I create sum of previous days?

I have this df with column t. I want to create columns t-1 and t-2 where t-1 gives the sum of two days and t-2 gives the sum of three days. 
  year      t  t-1 t-2
19620101    1   NA  NA
19630102    2   3   NA
19640103    3   5   6
19650104    4   7   9
19650104    5   9   12
19650104    6   11  15

How can I do this?
Extra question:
Can I do this automatically where I mention acc=3 and the code creates two columns with names (t-1),(t-2) or t1,t2 etc? Because my accumulations vary a lot, having an automatic solution help me a great deal. Thanks!'
Expected Output #2
 year      t  t-1 t-2
19620101    1   NA  NA
19630102    2   NA   NA
19640103    3   3   NA
19650104    4   5   6
19650104    5   7   9
19650104    6   9  12

A:

It seems like you already working with data.table, so you could easily combine it with zoo::rollsum while setting whatever index you want. For example:
library(zoo)
library(data.table)#v1.9.5+
indx <- 2:5 
setDT(df1)[, paste0("t", indx - 1L) := 
             lapply(indx, function(x) rollsum(t, x,
                             na.pad = TRUE, align='right'))]
df1
#        year t t1 t2 t3 t4
# 1: 19620101 1 NA NA NA NA
# 2: 19630102 2  3 NA NA NA
# 3: 19640103 3  5  6 NA NA
# 4: 19650104 4  7  9 10 NA
# 5: 19650104 5  9 12 14 15
# 6: 19650104 6 11 15 18 20

Or with base R/zoo
df1[paste0('t', indx - 1L)] <- lapply(indx, function(x) rollsum(df1$t,
                                   x, na.pad= TRUE, align='right'))
df1
#       year t t1 t2 t3 t4
# 1 19620101 1 NA NA NA NA
# 2 19630102 2  3 NA NA NA
# 3 19640103 3  5  6 NA NA
# 4 19650104 4  7  9 10 NA
# 5 19650104 5  9 12 14 15
# 6 19650104 6 11 15 18 20

Or using only base R functions
 df1[paste0('t', indx - 1L)] <-  lapply(indx, function(x) 
                     as.vector(stats::filter(df1$t, rep(1,x), sides=1)))

Update
Based on the new output, we can use the devel version of data.table, which has shift with type options for lead or lag.  The default is lag
indx <- 2:3
setDT(df1)[, paste0('t', indx - 1L) := lapply(indx, function(x) 
         shift(rollsum(t,x, na.pad=TRUE, align='right')))]
#      year t t1 t2
#1: 19620101 1 NA NA
#2: 19630102 2 NA NA
#3: 19640103 3  3 NA
#4: 19650104 4  5  6
#5: 19650104 5  7  9
#6: 19650104 6  9 12

data
df1 <- structure(list(year = c(19620101L, 19630102L, 19640103L, 
19650104L, 
19650104L, 19650104L), t = 1:6), .Names = c("year", "t"),
row.names = c(NA, -6L), class = "data.frame")

