title: Using lapply and the ifelse function in R
body: <p>I'm having an issue in R where I can run an ifelse statement on elements in a list, but as soon as I place the ifelse statement within the lapply function, it no longer works. </p>
  
  <p>Here's my example. I'm working with a list of three dataframes:</p>
  
  <pre><code>&gt; dflist
  [[1]]
    ID1 tID1
  1  m1    1
  2  m2    2
  3  m3    3
  4  m4    4
  5  m5    5
  
  [[2]]
    ID2 tID2
  1  m7    7
  2  m8    8
  3  m9    9
  4 m10   10
  5 m11   11
  
  [[3]]
    ID3 tID3
  1 m13   13
  2 m14   14
  3 m15   15
  4 m16   16
  5 m17   17
  6 m18   18
  </code></pre>
  
  <p>If a dataframe has an odd number of rows, I want R to label it "ODD". If the dataframe has an even number of rows,  I just want R to output the same dataframe. I would like the output to be a list.</p>
  
  <p>This works when I write standalone ifelse statements:</p>
  
  <pre><code>&gt; ifelse(nrow(dflist[[1]])%%2==!0, "ODD", dflist[1])
  [1] "ODD"
  
  &gt; ifelse(nrow(dflist[[3]])%%2==!0, "ODD", dflist[3])
  [[1]]
    ID3 tID3
  1 m13   13
  2 m14   14
  3 m15   15
  4 m16   16
  5 m17   17
  6 m18   18
  </code></pre>
  
  <p>But I get an error message as soon I put it into an lapply statement.</p>
  
  <pre><code>&gt; lapply(dflist, function(x) ifelse(nrow(dflist[[x]])%%2==!0, "ODD", dflist[x]))
  
   Error in dflist[[x]] : invalid subscript type 'list' 
  &gt; 
  </code></pre>
  
  <p>Any ideas for why this happens and how to fix it? Thank you</p>