title: Different output from code used in function and used alone in R
body: <p>I've been working with this kind of data to evaluate some stocks for my diploma thesis</p>
  
  <pre><code>library(quantmod)
  getSymbols("NOK",from="2012-01-01")
  data&lt;-NOK
  p1&lt;-4 
  dC&lt;-data[,4]
  dO&lt;-data[,1]
  emaC&lt;-EMA(dC,n=p1)
  w1&lt;-1.1
  Profit_L_1&lt;-((lag(dC,-1)-lag(dO,-1))/(lag(dO,-1)))*100
  Profit_L_2&lt;-((lag(dC,-2)-lag(dO,-1))/(lag(dO,-1)))*100
  Profit_L_3&lt;-((lag(dC,-3)-lag(dO,-1))/(lag(dO,-1)))*100
  Profit_L_4&lt;-((lag(dC,-4)-lag(dO,-1))/(lag(dO,-1)))*100
  Profit_L_5&lt;-((lag(dC,-5)-lag(dO,-1))/(lag(dO,-1)))*100
  Profit_L_all&lt;-ifelse(Profit_L_1&gt;w1,Profit_L_1,
                       ifelse(Profit_L_2&gt;w1,Profit_L_2,
                              ifelse(Profit_L_3&gt;w1,Profit_L_3,
                                     ifelse(Profit_L_4&gt;w1,Profit_L_4,
                                            ifelse(Profit_L_5&gt;w1,Profit_L_5,Profit_L_5)))))
  ChoosedN&lt;-ifelse(Profit_L_all==Profit_L_1,1,
                   ifelse(Profit_L_all==Profit_L_2,2,
                          ifelse(Profit_L_all==Profit_L_3,3,
                                 ifelse(Profit_L_all==Profit_L_4,4,
                                        ifelse(Profit_L_all==Profit_L_5,5,0)))))
  
  Profit_day&lt;-Profit_L_all/ChoosedN
  Winloss&lt;-ifelse(Profit_day&gt;0.3,1,
                  ifelse(Profit_day&lt;=0.3,0,"nothing"))
  Pos_emaC_dC_D&lt;-emaC&lt;dC
  Pos_emaC_dC_UP&lt;-emaC&gt;dC
  frame&lt;-data.frame(Pos_emaC_dC_UP,Pos_emaC_dC_D,Profit_L_all,ChoosedN,Profit_day,Winloss)
  colnames(frame)&lt;-c("Pos_emaC_dC_UP","Pos_emaC_dC_D","Profit_L_all","ChoosedN","Profit_day","Winloss")
  frame&lt;-frame[complete.cases(frame),]
  </code></pre>
  
  <p>Output of it is the data.frame called frame which I work with. It is clear without any NA's.</p>
  
  <p>I have written this function to quick evaluate  combinations of variables in this frame</p>
  
  <pre><code>quickmycomb&lt;-function (x,y){
    combination&lt;-deparse(substitute(y))
    df&lt;-subset(x,y)
    Success&lt;-length(subset(df,Winloss==1)$Winloss)/length(df$Winloss)
    absnumber&lt;-nrow(df)
    relnumber&lt;-nrow(df)/nrow(frame)
    sl&lt;-data.frame(Success,absnumber,relnumber)
    return(sl)
  } 
  </code></pre>
  
  <p>and when I use this function like</p>
  
  <pre><code>quickmycomb(frame,lag(Pos_emaC_dC_D,1) &amp; Pos_emaC_dC_UP)
  </code></pre>
  
  <p>It says that the output is frame with 86 rows in this case</p>
  
  <pre><code>    Success absnumber relnumber
  1 0.6162791        86 0.1396104
  </code></pre>
  
  <p>But when I use the code from this function 
  subset(frame,"combination") with the same combination of variables like</p>
  
  <pre><code>subset(frame,lag(Pos_emaC_dC_D,1) &amp; Pos_emaC_dC_UP)
  </code></pre>
  
  <p>It says that the output is data.frame with 0 rows!!! 
  I don't understand it, I suppose that the right output is the second, but I don't know why it doesn't work in the function.
  Any advices where should be the problem?</p>