title: Multiply values of some columns of a data frame within a date range and based on the values of another column in R
body: <p>I am struggling with an ifelse statement in R. I have a data frame with 64 columns and 12861 rows. I need to multiply 10 of those columns within a date range (date is a column) and based on the value in another row (tdiff) and replace the old values with the new values in the data frame. This line of code results in a dataframe that is 12681 X 12681. It should be the original size of 12681 (rows) by 64 (columns). What am I doing wrong? Thank you in advance!</p>
  <pre><code>test&lt;-data.frame(ifelse(slopejoin$tdiff==2 &amp; slopejoin$date&gt;=&quot;2021-01-30&quot; &amp; slopejoin$date&lt;=&quot;2021-08-08&quot;,
       slopejoin[,c(&quot;a_Pinene&quot;,&quot;Isoprene&quot;,&quot;Methyl_Vinyl_Ketone&quot;,&quot;Nopinone&quot;,&quot;Benzene_&quot;, &quot;D_Limonene&quot;,
                    &quot;Sabinene&quot;,&quot;a_Cedrene&quot;,&quot;Pentane&quot;,&quot;Hexane&quot;)]*0.85,
       slopejoin[,c(&quot;a_Pinene&quot;,&quot;Isoprene&quot;,&quot;Methyl_Vinyl_Ketone&quot;, &quot;Nopinone&quot;,&quot;Benzene_&quot;, &quot;D_Limonene&quot;,
                    &quot;Sabinene&quot;,&quot;a_Cedrene&quot;,&quot;Pentane&quot;,&quot;Hexane&quot;)]))
  </code></pre>
  <p>Edit: I took the first commentor's suggestion but wanted to add one more condition but received an error: &quot;Error in cols &amp; slopejoin$substr != z :
  operations are possible only for numeric, logical or complex types.&quot; Here is the code I am using:</p>
  <pre><code>cols &lt;- c(&quot;a_Pinene&quot;,&quot;Isoprene&quot;,&quot;Methyl_Vinyl_Ketone&quot;, &quot;Nopinone&quot;,&quot;Benzene_&quot;, &quot;D_Limonene&quot;, &quot;Sabinene&quot;,&quot;a_Cedrene&quot;,&quot;Pentane&quot;,&quot;Hexane&quot;)
  x &lt;- &quot;2021-01-06&quot;
  y &lt;- &quot;2021-08-08&quot;
  z &lt;- 'zero_VOC_2'
  
  slopejoin[slopejoin$tdiff == 2 &amp; slopejoin$date &gt;= x &amp; slopejoin$date &lt;= y,cols &amp; slopejoin$substr != z] &lt;- slopejoin[slopejoin$tdiff == 2 &amp; slopejoin$date &gt;= x &amp; slopejoin$date &lt;= y,cols &amp; slopejoin$substr != z] * 0.06
  </code></pre>
  <p>Not quite sure where the error comes from. First I was thinking of a class issue but it seems that all the conditions are checked with character class. I tried changing all instances of 'zero_VOC_2&quot; to as.numeric(1) or TRUE and checking for those in the conditional but to no avail. Any assistance is appreciated!</p>