title: Nested ifelse calls inside dplyr::mutate returns same values for each row
body: <p>Dear stackoverflow community,</p>
  
  <p>I am still a beginner in R and have encountered the following issue that I can't find a solution to on stackoverflow or the wider web. It seems pretty straight forward to me but I have no idea what I am missing or which coding conventions I am violating. The problem below is part of a larger function but the example below reproduces the issue.</p>
  
  <p>I have two data frames a and b and would like to create a new variable foo1 in a using a nested ifelse statement with conditions based on elements from both a and b. </p>
  
  <pre><code>a &lt;- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
  b &lt;- data.frame(foo=c(1,NA,2,3), bar=c(1,2,3,4))
  
  a &lt;- mutate(a, foo1 = ifelse(is.na(b$foo[1]), NA,
                               ifelse(a$foo == "a", "a", "f")))
  </code></pre>
  
  <p>What I would expect or am looking for is the following:
  The first ifelse statement checks whether the value in the first row of b is NA.
  Since it is not in this case, it should jump to the second ifelse statement and give me</p>
  
  <pre><code>a &lt;- data.frame(foo=c(1,NA,2,3), bar=c(1,2,3,4), foo1=c("a","f","f","f"))
  </code></pre>
  
  <p>since the first row of a$foo is a and the others are not a (b,c,d).</p>
  
  <p>What it does give me instead is</p>
  
  <pre><code>a &lt;- data.frame(foo=c(1,NA,2,3), bar=c(1,2,3,4), foo1=c("a","a","a","a"))
  </code></pre>
  
  <p>It prints "a" in all rows of foo1 instead of recognising that rows 2 to 4 should be assigned the else statement and thus "f". Is this due to the different dimensions of the ifelse conditions, i.e. the first ifelse condition is based on a single element whereas the second is supposed to evaluate every row of a$foo individually, which it does not seem to do. </p>
  
  <p>The larger function not shown here uses an is.na() condition inside the first ifelse loop. I am, however, suspecting that it's not due to the is.na statement but more likely due to the fact that I am using two ifelse conditions that call elements from two different data frames. </p>
  
  <p><strong>UPDATE:</strong> Prem's solution to add rowwise() to the pipe fixed the issue for the simplified example given above but unfortunately not for the more complicated example. The more complicated example uses lapply to apply the function to a list of data frames (a,b,c and d). As in the simplified example above it uses a second data frame as a look up table for the first ifelse statement. Here is the code:</p>
  
  <pre><code>a &lt;- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
  b &lt;- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
  c &lt;- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
  d &lt;- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
  LookUp &lt;- data.frame(foo=c(1,NA,2,3), bar=c("a","b","c","d"))
  
  List &lt;- list(a,b,c,d)
  names(List) &lt;- c("a","b","c","d")
  
  library(dplyr)
  
  List2 &lt;- lapply(seq_along(List), function(i) {
    a &lt;- filter(LookUp, bar == names(List[i]))
  temp &lt;- List[[i]] %&gt;%
    rowwise() %&gt;%
    mutate(foo1 = ifelse(is.na(LookUp$foo[1]), NA,
                         ifelse(List[[i]]$foo == "a", "a", "f"))) %&gt;%
    data.frame()
  } )
  </code></pre>
  
  <p>Now for all data frames all values in the new column foo1 are assigned the value "a". What I want is foo1 = c("a","f","f","f") for all list elements except list element 2, which should give me foo1 = c(NA, NA, NA, NA) given the first ifelse statement.</p>
  
  <p>Also, some of the data frames in my list are very large. Rowwise() substantially slows down the function. Is there a better/faster way to code my function?</p>
  
  <p><strong>UPDATE 2:</strong> My apologies for further complicating this issue. Prem's second solution using <code>Map()</code> worked a charm for the example I had given. Unfortunately, I made a mistake in my more complex example. I specified to use <code>is.na(LookUp$foo[1])</code> in the first <code>ifelse</code> statement instead of <code>is.na(a$foo[1])</code> as intended. a is the subset lookup table storing information about the variable names for each element of my list. If I change my code to <code>is.na(a$foo[1])</code>, however, the <code>Map()</code> solution no longer works because the function does not specify how to cycle through i. I want the code to subset the lookup table differently for each function run. The updated value of <code>b$bar</code> should therefore be <code>c(NA,NA,NA,NA)</code>
  Code below is the updated one. </p>
  
  <pre><code>a &lt;- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
  b &lt;- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
  c &lt;- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
  d &lt;- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
  LookUp &lt;- data.frame(foo=c(1,NA,2,3), bar=c("a","b","c","d"))
  
  List &lt;- list(a,b,c,d)
  names(List) &lt;- c("a","b","c","d")
  
  library(dplyr)
  
  List2 &lt;- lapply(seq_along(List), function(i) {
    LookUp2 &lt;- filter(LookUp, bar == names(List[i]))
  temp &lt;- List[[i]] %&gt;%
    rowwise() %&gt;%
    mutate(foo1 = ifelse(is.na(LookUp2$bar[1]), NA,
                         ifelse(List[[i]]$foo == "a", "a", "f"))) %&gt;%
    data.frame()
  } )
  </code></pre>
  
  <p>I tried to add name as a second vector that allows me to dynamically change i in my function following what has been suggested in this post <a href="https://stackoverflow.com/questions/25006076/how-do-i-extract-the-index-or-name-of-the-list-item-within-fun-of-lapply">How do I extract the index or name of the list item within FUN of lapply?</a> but without success. It continues to give me identical row values within and between list elements.
  Thanks for your help and patience. </p>
  
  <pre><code>a &lt;- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
  b &lt;- data.frame(foo=c("a","a","c","d"), bar=c("e","f","g","h"))
  c &lt;- data.frame(foo=c("a","a","a","d"), bar=c("e","f","g","h"))
  d &lt;- data.frame(foo=c("a","b","a","d"), bar=c("e","f","g","h"))
  LookUp &lt;- data.frame(foo=c(1,NA,2,3), bar=c("a","b","c","d"))
  List &lt;- list(a,b,c,d)
  names(List) &lt;- c("a","b","c","d")
  
  library(dplyr)
  List_new &lt;- Map(function(x, name) {
    i = which(LookUp$bar == name)
    Lookup2 &lt;- filter(LookUp, bar == names(List[i]))
    x %&gt;% 
      rowwise() %&gt;%
      mutate(foo1=ifelse(is.na(Lookup2$bar[1]), NA,
                         ifelse(foo == "a", "a", "f")))
  }, List, names(List))
  List_new
  </code></pre>
  
  <p>Any help would be very much appreciated.</p>