title: Types and comparisons in R
body: <p>I've been working with R for a month or so, and my comprehension of some subtleties is still quite superficial.
  I have had an issue, which I managed to solve (details below), but I still can't explain precisely why it did not work with the first solution.
  Note that the example below makes no practical sense for I have simplified it as much as possible so that the problem is quite clear.</p>
  
  <p>ISSUE :
  Given a data frame with 4 columns (email, first, last, company) :</p>
  
  <pre><code>&gt; users &lt;- data.frame(matrix(vector(), 0, 4, dimnames=list(c(), c("email", "first", "last", "company"))), stringsAsFactors=F)
  &gt; users[1,] &lt;- c("robert@redford.com", "Robert", "Redford", "Paramount")
  &gt; users[2,] &lt;- c("julia@roberts.com", "Erin", "B.", "Hinkley")
  &gt; users[3,] &lt;- c("matt@damon.com", "Will", "H.", "Stanford")
  &gt; users[4,] &lt;- c("john@malkovitch.com", "John", "M.", "JM")
  </code></pre>
  
  <p>I take one particular row :</p>
  
  <pre><code>&gt; user &lt;- users[3,]
  </code></pre>
  
  <p>When I try to subset the dataframe on a criteria which could have lead to return the previously mentioned row, it returns no result.</p>
  
  <pre><code>&gt; users[users$email == user["email"],]
  [1] email   first   last    company
  &lt;0 lignes&gt; (ou 'row.names' de longueur nulle)
  </code></pre>
  
  <p>I instantly thought it was a casting issue (sorry for this bad one)</p>
  
  <pre><code>&gt; users[users$email == as.character(user["email"]),]
             email first last  company
  3 matt@damon.com  Will   H. Stanford
  </code></pre>
  
  <p>However, when I tried to figure out where exactly the issue was, and tried this :</p>
  
  <pre><code>&gt; users[users$email == "matt@damon.com",]
             email first last  company
  3 matt@damon.com  Will   H. Stanford
  
  &gt; user["email"] == "matt@damon.com"
    email
  3  TRUE
  
  &gt; users[3,]$email == user$email
  [1] TRUE
  </code></pre>
  
  <p>I got quite confused :</p>
  
  <ul>
  <li>First, I thought about it as a math problem : <code>if A == B and B == C, then A == C</code> (according to Captain Obvious). So, just replacing a member A by another member B which is supposed to be equal to A (given the "TRUE" statement) in some expression should have no impact on the result of this expression.</li>
  <li><code>3  TRUE != [1] TRUE</code>. I think <code>[1] TRUE</code> is a logical vector of size 1 which first element is TRUE. <code>3  TRUE</code> is (1x1) matrix row, which column "email" value is TRUE.</li>
  </ul>
  
  <p>My problem is with consistency : either two objects of equal content but different types should be equal, or they should be different. I have a problem with "Sometimes there is type inference, and sometimes not". Is there a rule I can't see beyond this behavior ? (I guess there is one)</p>
  
  <p>Another expression of the behavior I'd like to get is this one :</p>
  
  <pre><code>&gt; unique(users$email) == "matt@damon.com"
  [1] FALSE FALSE  TRUE FALSE
  &gt; unique(users$email) == user["email"]
    email
  3 FALSE
  </code></pre>
  
  <p>Obviously R does get what I want (considering the fact that it gives me the matching row). But I can't explain (nor use) the result of the second statement.
  Any explanations / thoughts?</p>