title: Trouble with identical and all.equal when comparing two floating point elements in two vectors
body: <p>This is a follow up to a question I posted <a href="https://stackoverflow.com/questions/15465505/relational-operator-is-not-producing-the-expected-answer-in-r"><strong>yesterday</strong></a>.  I can't seem to get things right about floating point comparison in R. Yesterday I was using <code>&gt;=</code> to compare two floating point values but that dint seem to get the right results.</p>
  
  <p>Today, I <a href="https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal"><strong>tried</strong></a> to run <code>all.equal</code>, element-wise, on two vectors, which yielded a <code>mean difference</code>, which does not work for this application. I need the comparison function <em>to return a vector</em>. Then, I found <code>identical</code> and combined it with <code>mapply</code>. This became more accurate, but not 100% accurate.  What am I doing wrong? Since this is financial data, should I be using a <a href="https://stackoverflow.com/questions/6874867/floating-point-issue-in-r"><strong>decimal data type</strong></a>?  If so, how?</p>
  
  <p><em>From yesterday's post (with updated code, reflecting current frustration):</em></p>
  
  <p><strong>The goal is to:</strong> read the data into a <code>data.frame</code>, take an average of yesterday's <code>High</code>, <code>Low</code>, and <code>Close</code> prices; and, compare today's Open price with yesterday's average.  </p>
  
  <p>After running the scripts on large data, I found that my results in <code>R</code> <strong>didn't match</strong> a similar analysis run in <code>Excel</code>. I've scaled down the problem to it's essential parts. My test file, <code>test.csv</code>, looks like this, including a new line at the end of the last row:</p>
  
  <pre><code>&lt;TICKER&gt;,&lt;DATE&gt;,&lt;TIME&gt;,&lt;OPEN&gt;,&lt;LOW&gt;,&lt;HIGH&gt;,&lt;CLOSE&gt;
  EURUSD,20020311,0:00:00,0.8733,0.873,0.877,0.8749
  EURUSD,20020312,0:00:00,0.8749,0.8704,0.876,0.8754
  EURUSD,20020313,0:00:00,0.8753,0.8725,0.878,0.8754
  EURUSD,20020314,0:00:00,0.8753,0.8752,0.8841,0.8823
  EURUSD,20020315,0:00:00,0.8823,0.8808,0.8868,0.8823
  EURUSD,20020318,0:00:00,0.8809,0.878,0.8828,0.8821
  EURUSD,20020319,0:00:00,0.8821,0.8796,0.884,0.8816
  EURUSD,20020320,0:00:00,0.8815,0.8786,0.8857,0.8855
  EURUSD,20020321,0:00:00,0.8854,0.8806,0.8857,0.8823
  </code></pre>
  
  <p>My Code:</p>
  
  <pre><code># Read in test file
  raw &lt;- read.csv('test.csv', header=TRUE, sep=",")
  
  # Convert date and dump dat into data frame
  stripday &lt;- strptime(raw$X.DATE, format="%Y%m%d")
  data &lt;- data.frame(stripday, raw)
  
  # Drop unused data columns and name used columns
  drops &lt;- c("X.DATE.", "X.TIME.", "X.TICKER.")
  data &lt;- data[, !(names(data) %in% drops)]
  colnames(data) &lt;- c("Date", "Open", "Low", "High", "Close")
  
  # Convert values from facotrs to numeric
  data[,2] &lt;- as.numeric(as.character(data[,2]))
  data[,3] &lt;- as.numeric(as.character(data[,3]))
  data[,4] &lt;- as.numeric(as.character(data[,4]))
  data[,5] &lt;- as.numeric(as.character(data[,5]))
  
  # Take average of High, Low, and Close 
  data[['Avg']] &lt;- NA
  data[['Avg']][2:9] &lt;- (
      data[['High']][1:8] + 
      data[['Low']][1:8] + 
      data[['Close']][1:8]) / 3
  
  # Is Open greater than or equal to Average
  data[['OpenGreaterThanOrEqualAvg']] &lt;- NA
  data[['OpenGreaterThanOrEqualAvg']][2:9] &lt;- 1 * (mapply(identical,data[['Open']][2:9], data[['Avg']][2:9]) | data[['Open']][2:9] &gt; data[['Avg']][2:9])
  
  # Write data to .csv
  write.table(data, 'output.csv', quote=FALSE, sep=",", row.names=FALSE)
  </code></pre>
  
  <p>Note that there should be a 1, not 0, for 3/14/2002.</p>