title: How to check for a presence of an element in a data frame and if it exists adding values to another data frame in R
body: <p>I have a data frame <code>df</code> with letters. Column number of <code>df</code> corresponds to a row number of <code>df2</code>. &quot;End&quot; column of <code>df2</code> includes rows 15:25 of <code>df</code>.</p>
  <p>I would like to check if there is &quot;D&quot; in a row 16 of <code>df</code> of any column. If there is &quot;D&quot; add 1 to <code>df2$End</code> of the corresponding row.
  For example, there is &quot;D&quot; in [16,2] of <code>df</code> then I want to add 1 to [2,3] <code>df2</code>.</p>
  <pre><code>df 
      V1 V2 V3 V4 V5 V6 V7 V8
  1   A  V  S  A  S  Y  A  E
  2   S  K  E  S  V  E  K  Q
  3   R  R  G  E  A  L  Q  A
  4   A  K  P  A  T  V  E  L
  5   G  R  G  S  Q  P  A  Q
  6   E  R  R  K  C  R  N  L
  7   A  S  P  K  D  L  P  D
  8   E  E  V  K  P  P  T  F
  9   E  D  L  V  E  G  L  T
  10  R  A  E  D  E  E  E  Y
  11  I  F  Y  N  I  S  Y  Q
  12  P  I  I  I  W  V  A  K
  13  M  T  E  D  V  F  A  L
  14  I  I  L  L  L  Q  L  Q
  15  S  S  V  T  S  I  V  V
  16  P  D  S  I  D  D  D  D
  17  S  S  S  E  S  D  D  M
  18  -  D  D  S  D  E  D  N
  19  -  G  D  S  -  N  S  R
  20  -  E  E  S  -  E  P  E
  21  -  E  E  D  -  D  S  I
  22  -  P  P  E  -  T  D  G
  23  -  K  S  E  -  T  E  K
  24  -  E  T  E  -  S  P  -
  25  -  E  S  D  -  E  K  -
  
  df2
              V1       V2         V3
  categ    Start    Middle      End
  V1       4.125    1.775      1.125
  V2       5.875    1.250      3.875
  V3       2.600    1.600      1.375
  V4        8.75     1.50       2.25
  V5       4.375    1.500      1.000
  V6       2.650    1.300      2.875
  V7       3.250    1.375      1.000
  V8       1.525    2.000      2.625
  
  dput(df)
  structure(list(V1 = c(&quot;A&quot;, &quot;S&quot;, &quot;R&quot;, &quot;A&quot;, &quot;G&quot;, &quot;E&quot;, &quot;A&quot;, &quot;E&quot;, 
  &quot;E&quot;, &quot;R&quot;, &quot;I&quot;, &quot;P&quot;, &quot;M&quot;, &quot;I&quot;, &quot;S&quot;, &quot;P&quot;, &quot;S&quot;, &quot;-&quot;, &quot;-&quot;, &quot;-&quot;, &quot;-&quot;, 
  &quot;-&quot;, &quot;-&quot;, &quot;-&quot;, &quot;-&quot;), V2 = c(&quot;V&quot;, &quot;K&quot;, &quot;R&quot;, &quot;K&quot;, &quot;R&quot;, &quot;R&quot;, &quot;S&quot;, 
  &quot;E&quot;, &quot;D&quot;, &quot;A&quot;, &quot;F&quot;, &quot;I&quot;, &quot;T&quot;, &quot;I&quot;, &quot;S&quot;, &quot;D&quot;, &quot;S&quot;, &quot;D&quot;, &quot;G&quot;, &quot;E&quot;, 
  &quot;E&quot;, &quot;P&quot;, &quot;K&quot;, &quot;E&quot;, &quot;E&quot;), V3 = c(&quot;S&quot;, &quot;E&quot;, &quot;G&quot;, &quot;P&quot;, &quot;G&quot;, &quot;R&quot;, 
  &quot;P&quot;, &quot;V&quot;, &quot;L&quot;, &quot;E&quot;, &quot;Y&quot;, &quot;I&quot;, &quot;E&quot;, &quot;L&quot;, &quot;V&quot;, &quot;S&quot;, &quot;S&quot;, &quot;D&quot;, &quot;D&quot;, 
  &quot;E&quot;, &quot;E&quot;, &quot;P&quot;, &quot;S&quot;, &quot;T&quot;, &quot;S&quot;), V4 = c(&quot;A&quot;, &quot;S&quot;, &quot;E&quot;, &quot;A&quot;, &quot;S&quot;, 
  &quot;K&quot;, &quot;K&quot;, &quot;K&quot;, &quot;V&quot;, &quot;D&quot;, &quot;N&quot;, &quot;I&quot;, &quot;D&quot;, &quot;L&quot;, &quot;T&quot;, &quot;I&quot;, &quot;E&quot;, &quot;S&quot;, 
  &quot;S&quot;, &quot;S&quot;, &quot;D&quot;, &quot;E&quot;, &quot;E&quot;, &quot;E&quot;, &quot;D&quot;), V5 = c(&quot;S&quot;, &quot;V&quot;, &quot;A&quot;, &quot;T&quot;, 
  &quot;Q&quot;, &quot;C&quot;, &quot;D&quot;, &quot;P&quot;, &quot;E&quot;, &quot;E&quot;, &quot;I&quot;, &quot;W&quot;, &quot;V&quot;, &quot;L&quot;, &quot;S&quot;, &quot;D&quot;, &quot;S&quot;, 
  &quot;D&quot;, &quot;-&quot;, &quot;-&quot;, &quot;-&quot;, &quot;-&quot;, &quot;-&quot;, &quot;-&quot;, &quot;-&quot;), V6 = c(&quot;Y&quot;, &quot;E&quot;, &quot;L&quot;, 
  &quot;V&quot;, &quot;P&quot;, &quot;R&quot;, &quot;L&quot;, &quot;P&quot;, &quot;G&quot;, &quot;E&quot;, &quot;S&quot;, &quot;V&quot;, &quot;F&quot;, &quot;Q&quot;, &quot;I&quot;, &quot;D&quot;, 
  &quot;D&quot;, &quot;E&quot;, &quot;N&quot;, &quot;E&quot;, &quot;D&quot;, &quot;T&quot;, &quot;T&quot;, &quot;S&quot;, &quot;E&quot;), V7 = c(&quot;A&quot;, &quot;K&quot;, 
  &quot;Q&quot;, &quot;E&quot;, &quot;A&quot;, &quot;T&quot;, &quot;P&quot;, &quot;T&quot;, &quot;L&quot;, &quot;E&quot;, &quot;Y&quot;, &quot;V&quot;, &quot;I&quot;, &quot;L&quot;, &quot;V&quot;, 
  &quot;D&quot;, &quot;D&quot;, &quot;D&quot;, &quot;S&quot;, &quot;P&quot;, &quot;S&quot;, &quot;D&quot;, &quot;E&quot;, &quot;P&quot;, &quot;K&quot;), V8 = c(&quot;E&quot;, 
  &quot;Q&quot;, &quot;A&quot;, &quot;L&quot;, &quot;Q&quot;, &quot;L&quot;, &quot;D&quot;, &quot;F&quot;, &quot;T&quot;, &quot;Y&quot;, &quot;Q&quot;, &quot;V&quot;, &quot;E&quot;, &quot;Q&quot;, 
  &quot;V&quot;, &quot;D&quot;, &quot;M&quot;, &quot;N&quot;, &quot;R&quot;, &quot;E&quot;, &quot;I&quot;, &quot;G&quot;, &quot;K&quot;, &quot;-&quot;, &quot;-&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -25L))
  
  dput(df2)
  structure(list(V1 = c(&quot;Start&quot;, &quot;4.125&quot;, &quot;5.875&quot;, &quot;2.600&quot;, 
  &quot;8.75&quot;, &quot;4.375&quot;, &quot;2.650&quot;, &quot;3.250&quot;, &quot;1.525&quot;), V2 = c(&quot;Middle&quot;, 
  &quot;1.775&quot;, &quot;1.250&quot;, &quot;1.600&quot;, &quot;1.50&quot;, &quot;1.500&quot;, &quot;1.300&quot;, &quot;1.375&quot;, 
  &quot;2.000&quot;), V3 = c(&quot;End&quot;, &quot;1.125&quot;, &quot;3.875&quot;, &quot;1.375&quot;, &quot;2.25&quot;, 
  &quot;1.000&quot;, &quot;2.875&quot;, &quot;1.000&quot;, &quot;2.625&quot;)), class = &quot;data.frame&quot;, row.names = c(&quot;categ&quot;, 
  &quot;V1&quot;, &quot;V2&quot;, &quot;V3&quot;, &quot;V4&quot;, &quot;V5&quot;, &quot;V6&quot;, &quot;V7&quot;, &quot;V8&quot;))
  </code></pre>
  <p>The desired output would be:</p>
  <pre><code> df3
                  V1       V2         V3
      categ    Start    Middle      End
      V1       4.125    1.775      1.125
      V2       5.875    1.250      4.875
      V3       2.600    1.600      1.375
      V4        8.75     1.50       2.25
      V5       4.375    1.500      2.000
      V6       2.650    1.300      3.875
      V7       3.250    1.375      2.000
      V8       1.525    2.000      3.625
  </code></pre>
  <p>I tried the following:</p>
  <pre><code>if (df[16,] == &quot;D&quot;) {
          df2 + 1
          print(df2)
  }
  </code></pre>
  <p>I am getting this warning message:</p>
  <pre><code>Warning message:
  In if (df[16, ] == &quot;D&quot;) { :
    the condition has length &gt; 1 and only the first element will be used
  </code></pre>