FROM: 
http://stackoverflow.com/questions/21372735/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    ID    Minutes Value
    xxxx  118     3 
    xxxx  121     4 
    xxxx  122     3 
    yyyy  122     6 
    xxxx  123     4 
    yyyy  123     8 

    ...   ...     ...





library(data.table)

## ~~~~~~~~~~~~~~ ##  
## SAMPLE DATA
## ~~~~~~~~~~~~~~ ##  
  set.seed(1)
  DT <- CJ(IDs = c("xxxx", "yyyy"), Minutes=100:250)
  DT[, value := sample(10, nrow(DT), TRUE)]

  # remove random Minutes
  DT <- DT[!sample(nrow(DT), 40)]
  DF <- as.data.frame(DT)
## ~~~~~~~~~~~~~~ ##  


  library(data.table)

## OPTION 1
## ~~~~~~~~~~~~~~ ##  
  ## Convert to data.table
  DT <- data.table(DF, key=c("IDs", "Minutes"))

  ## Missing Minues will be added in. Value will be set to NA. 
  DT <- DT[CJ(unique(IDs), seq(min(Minutes), max(Minutes)))]

  ## Run your function
  DT[, rollapply(value, 60, mean, na.rm=TRUE), by=IDs]

## OPTION 2
## (same thing, but non-modifying of DT)
## ~~~~~~~~~~~~~~ ##  
### Alternatively, you don't need to keep the 'padded' Minutes / NA Values: 
You can do it all in one shot:

    ## Convert your DF to a data.able
    DT <- data.table(DF, key=c("IDs", "Minutes"))

    ## Compute rolling means, with on-the-fly padded minutes
    DT[ CJ(unique(IDs), seq(min(Minutes), max(Minutes))) ][, 
      rollapply(value, 60, mean, na.rm=TRUE), by=IDs]
