R Vector Values Overwrite in Function : <p><strong>specific problem i'm solving:</strong> </p>

<p>Create a character vector with length of number-of-rows-of-iris-dataset, such that, each element gets a character value – “greater than 5″ if the corresponding ‘Sepal.Length’ > 5, else it should get “lesser than 5″.</p>

<p>This works: </p>

<pre><code>char_vect &lt;- c(seq(1:nrow(iris)))

for (i in char_vect) {
  if (iris$Sepal.Length[i] &gt; 5) {
    char_vect[i] &lt;- ("greater than 5") } else {
      char_vect[i] &lt;- ("less than 5")
    }
}
</code></pre>

<p>But for some reason, if I define char_vect as follows, I get funny responses:</p>

<pre><code>char_vect &lt;- iris$Sepal.Length

for (i in char_vect) {
  if (iris$Sepal.Length[i] &gt; 5) {
    char_vect[i] &lt;- ("greater than 5") } else {
      char_vect[i] &lt;- ("less than 5")
    }
}
</code></pre>

<p>Although the vectors I'm starting off with are different in the two cases, the function is the same and is overwriting the original vectors so shouldn't their values be irrelevant as long as their lengths are equivalent?</p>

<p>When I look at what the output of the second option is, I'm even more confused since it looks like some the function overwrote some of the original values but not all of them ... </p>

<p>My hunch is that this has to do with the fact that option 1 defines char_vect as integers while option 2 is numeric? But I don't understand why a comparison operator wouldn't work equally on integers and numeric. Looking at the output of option 2, there also seems to be integers that the function did not work on. </p>

<p>much thank from a R novice!</p>
