title: Efficient Manipulation of Time Series in R Data Table Package
body: <p><em>Much appreciated for your comment/answer.</em></p>
  
  <p><strong>Context:</strong> I have a large data table of daily prices of swap rates across a dozen countries. The columns are [ID, Date, X1Y, X2Y, X3Y ... X30Y], where X..Y are columns indicating the part of the yield curve (e.g. X1Y is 1-year swap, X3Y is 3-year swap). The two keys are <em>ID</em> (e.g. "AUD", "GBP") and <em>Date</em> (e.g. "2001-04-13", "2001-04-16").</p>
  
  <p><strong>Dummy Data:</strong></p>
  
  <pre><code>set.seed(123)
  dt &lt;- cbind(ID=rep(c("AUD","GBP"),c(100,100)),X1Y=rnorm(200),X2Y=rnorm(200),X3Y=rnorm(200))
  dt &lt;- data.table(dt)
  dt[,Date := seq(from=as.IDate("2013-01-01"), by="1 day", length.out=100)]
  setkeyv(dt,c("ID","Date"))
  </code></pre>
  
  <p><strong>Problem 1:</strong>
  First generate some dummy signals. What's the syntax if there is 100 columns with fairly complicated signal generation formula coded in a separate function say genSig(X1Y)? Here's what I mean using just the 3 columns and some meaningless formula:</p>
  
  <pre><code>dt[,SIG1  :=c(0, diff(X1Y ,1)),by="ID"]
  dt[,SIG2  :=c(0, diff(X2Y ,1)),by="ID"]
  dt[,SIG3  :=c(0, diff(X3Y ,1)),by="ID"]
  </code></pre>
  
  <p><strong>Problem 2:</strong>
  Carry forward column(s) based on "middle of the month". For example, using the SIG columns, I'd like to make everything after say the 15th of each month the same as the signal on the 15th, until next month's 15th. The tricky thing is that the actual data contains just trading days so some months do not have 15th if it is a weekend/holiday. Another issue is using an efficient syntax, I could achieve something similar using loop (I know..) for the start of each month just to show what I meant:</p>
  
  <pre><code>for (i in 2:length(dt$Date)){
    if(as.POSIXlt(dt[i,]$Date)$mon == as.POSIXlt(dt[i-1,]$Date)$mon){
      dt[i, SIG1 := dt[i-1,SIG1]] 
      dt[i, SIG2 := dt[i-1,SIG2]] 
      dt[i, SIG3 := dt[i-1,SIG3]] 
    }
  }
  </code></pre>
  
  <p>I can't figure out how to deal with the "mid-month" issue since it can fall on the 15th or 16th or 17th. Like Problem 1, would appreciate if there is a smart way to insert/update multiple/dozen columns.</p>