title: how to iterate a function on every row that function takes inputs from various datasets
body: <p>I have three data sets all in xts format. I have created a function that takes multiple arguments and that function takes input from these three data sets. I want to iterates this function on every row of all these data frames at once. I can do it manually one by one like i have shown in code segment but this will take a lot of time. Please suggest be better approach.</p>
  
  <pre><code>time_series_momentum&lt;-function(xts.ret,xts.rank,n=1){
    # for trading the top ranked asset, replace all ranks above n
    # with NA to set up for element wise multiplication to get
    # the realized returns
    xts.rank &lt;- as.matrix(xts.rank)
    xts.rank[xts.rank &gt; n] &lt;- NA
    # set the element to 1 for assets ranked &lt;= to rank
    xts.rank[xts.rank &lt;= n] &lt;- 1
    # element wise multiplication of the
    # 1 period return matrix and lagged rank matrix
    mat.ret &lt;- as.matrix(xts.ret) * xts.rank
  
    # average the rows of the mat.ret to get the
    # return for that period
    vec.ret &lt;- rowMeans(mat.ret, na.rm = TRUE)
  
  
    # convert to an xts object
    vec.ret &lt;- xts(x = vec.ret, order.by = index(xts.ret))
    f &lt;- list(mat = mat.ret, ret = vec.ret, rank = xts.rank)
    return(f)
  }
  
  test1&lt;-time_series_momentum(xts.ret[1,],xts.rank[1,],n=number_xts[1,])
  test2&lt;-time_series_momentum(xts.ret[2,],xts.rank[2,],n=number_xts[2,])
  and so on
  </code></pre>