title: left join a variable by id in R
body: <p>I have two tables df1 and df2. Now I want to left join a variable <code>newVar</code> of df2 to df1 by the same id. But the problem is that there are some ids exist in df1 but not in df2. </p>
  
  <p>So I want that <code>if df1.id exists in df2, then df1.newVar=df2.newVar, else df1.newVar=0</code>.</p>
  
  <p>In R, I've tried <code>df1$newVar= ifelse((df1$id %in% df2$id), df2$newVar, 0)</code> but the result was not good.</p>
  
  <p>I've tried also <code>sqldf('select df1.*, df2.newVar as newVar from df1 left outer join df2 on df2.id= df1.id')</code> but R gives an error.</p>
  
  <p>Here is an example of df1:</p>
  
  <pre><code>id   var1
  A     1
  A     2
  B     1
  </code></pre>
  
  <p>df2:</p>
  
  <pre><code>id   newVar
  A     1
  A     1
  </code></pre>
  
  <p>I want the result of df1 looks like:</p>
  
  <pre><code>id   var1   newVar
  A     1       1
  A     2       1
  B     1       0
  </code></pre>