Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a variable from a condition with more than 2 arguments
    primarykey
    data
    text
    <p>Following is an easy version of what I'm trying to do. I have the following vector:</p> <pre><code>wage = 1:10 # Generate a sequence from 1 to 10 </code></pre> <p>And I want to create another vector <code>wage_level</code> such that:</p> <p>(i) <code>wage_level</code> is <code>"low"</code> if <code>wage</code> less than <code>5</code></p> <p>(ii) <code>wage_level</code> is <code>normal</code> if <code>wage</code> is equal to <code>5</code></p> <p>(iii) <code>wage_level</code> is <code>high</code> if <code>wage</code> is greater than <code>5</code></p> <p>I know I can use nested <code>ifelse</code> statements to do it, however, as I pointed out earlier, this is but a simplified version of what I really want to do because I have about 15 alternatives.</p> <p><strong>Edit</strong></p> <p>The answer provided below makes use of the <code>cut()</code> function, which actually works well in many cases. However, it does not seem to "work" in my case. Following is the detailed explanation.</p> <p>I was able to use the <code>cut()</code> function to create the <code>wage_level</code> vector:</p> <pre><code>wage = runif(10, 1, 10) # Randomly generate 10 values between 1 and 10 # Here I use the cut() function wage_level = cut(wage, breaks = c(1, 4, 6, 10), labels = c("low", "normal", "high"), include.lowest = TRUE) &gt; wage [1] 5.522422 4.793292 8.161671 5.480415 1.396909 3.403013 4.940242 7.762142 6.364159 4.603998 &gt; wage_level [1] normal normal high normal low low normal high high normal Levels: low normal high </code></pre> <p>Now, let's suppose I want to use the <code>wage_level</code> vector to create another vector (the <code>rating</code> vector) using the <code>cut()</code> function. The condition to create the <code>rating</code> vector is as follows:</p> <p>(i) <code>rating</code> is <code>"1"</code> if <code>wage_level</code> less than <code>"low"</code></p> <p>(ii) <code>rating</code> is <code>2</code> if <code>wage_level</code> is equal to <code>"normal"</code></p> <p>(iii) <code>rating</code> is <code>3</code> if <code>wage_level</code> is greater than <code>"high</code></p> <p>My problem is that using the <code>cut()</code> function will not make the <code>rating</code> vector a <code>numeric</code> vector will the values of my choice. The following code does not work:</p> <pre><code>rating = cut(as.numeric(wage_level), breaks = c(0, 1, 2, 3), labels = c(1.2, 6.5, 8.9), include.lowest = TRUE) &gt; as.numeric(rating) [1] 2 2 3 2 1 1 2 3 3 2 </code></pre> <p>I mainly have two problems here:</p> <p>(i) I would have preferred a way to use the actual strings (i.e. "low", "normal" and "high") instead of the labels indexes</p> <p>(ii) The values in the <code>rating</code> vector have nothing to do with the values I specified.</p> <p>Any other method to achieve the desired result?</p> <p>Thank you very much for your help :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload