title: Why didn't mutate fill all rows? Was using mutate and ifelse to look up imputed values from another dataframe
body: <p>Here is the deal. Was trying to use <code>mutate</code> from the <code>plyr</code> package to look up an appropriate value from another dataframe, if, the <code>v</code> variable in the original dataframe was <code>NA</code>. The looked up value is supposed to go into a new variable <code>imputed</code>. I also defined a custom function for this look up purpose.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>if(!require(plyr)){
    install.packages("plyr")
    library(plyr)
  }
  df = data.frame(d=c(1,1,1,2,2,2,3,3,3),
                  g=rep(c(1,2,3),3),
                  v=c(5,NA,NA,5,NA,NA,5,NA,NA))
  imputed = data.frame(g=c(1,2,3),
                       v=c(5,10,15))
  getImputed = function(p){
    imputed[imputed$g==p,"v"]
  }
  df = mutate(df,imputed=ifelse(is.na(v),getImputed(g),v))
  df
  </code></pre>
  
  <p>And this is the resulting dataframe:</p>
  
  <pre><code>  d g  v imputed
  1 1 1  5       5
  2 1 2 NA      10
  3 1 3 NA      15
  4 2 1  5       5
  5 2 2 NA      NA
  6 2 3 NA      NA
  7 3 1  5       5
  8 3 2 NA      NA
  9 3 3 NA      NA
  </code></pre>
  
  <p>As one can see, only the first 3 rows were successfully filled in by mutate. It is likely that the <code>ifelse</code> function is the issue, but I can't see why : (</p>
  
  <p>What is weird is that, if the <code>imputed</code> dataframe has 4 rows, like this:</p>
  
  <pre><code>imputed = data.frame(g=c(1,2,3,4),
                       v=c(5,10,15,20))
  </code></pre>
  
  <p>then the <code>df</code> dataframe was filled up properly:</p>
  
  <pre><code>  d g  v imputed
  1 1 1  5       5
  2 1 2 NA      10
  3 1 3 NA      15
  4 2 1  5       5
  5 2 2 NA      10
  6 2 3 NA      15
  7 3 1  5       5
  8 3 2 NA      10
  9 3 3 NA      15
  </code></pre>
  
  <p>but R gave me a warning saying:</p>
  
  <pre><code>Warning message:
  In imputed$g == p :
    longer object length is not a multiple of shorter object length
  </code></pre>
  
  <p>Am I overlooking something?</p>