Adding new column with conditional values using ifelse
<p>I have a data frame with more than 400.000 observations and I'm trying to add a column to it which its values depend on another column and sometimes multiple ones.</p>

<p>Here is a simpler example of what I'm trying to do :</p>

<pre><code># Creating a data frame 

M &lt;- data.frame(c("A","B","C"),c(5,100,60))

names(M) &lt;- c("Letter","Number")

#adding a column 

M$Size &lt;- NA

# if Number &lt;= 50 Size is small, 
# if Number is between 50 and 70, Size is Medium
# if Number is Bigger than 70, Size is Big

ifelse (M$Number &lt;=50, M$Size &lt;-"Small",
        ifelse(M$Number &lt;= 70,
        M$Size &lt;- "Medium",
        M$Size &lt;- "Big"
        ))
</code></pre>

<p>When I run the Code, the output I get is :</p>

<pre><code>[1] "Small"  "Big"    "Medium"
</code></pre>

<p>But the "Size" column in M is always the last condition in the ifelse function :</p>

<pre><code>&gt; print (M)
  Letter Number Size
1      A      5  Big
2      B    100  Big
3      C     60  Big
</code></pre>

<p><strong>The Result that I want :</strong></p>

<pre><code>&gt; print (M)
  Letter Number Size
1      A      5  Small
2      B    100  Big
3      C     60  Medium
</code></pre>

<p>I can solve the problem by subsetting  each condition<code>subset</code> and using <code>rbind</code> to get the result I want but the code will be very long and since the original data frame I'm working on is big, it'll take more time to run. So I'm wondering how can I fix this issue ? </p>