Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating categorical variables from mutually exclusive dummy variables
    primarykey
    data
    text
    <p>My question regards an elaboration on a previously answered question about <a href="https://stackoverflow.com/questions/8419357/combine-multiple-categorical-variables-in-one-dummy-variable">combining multiple dummy variables into a single categorical variable</a>. </p> <p>In the question previously asked, the categorical variable was created from dummy variables that were NOT mutually exclusive. For my case, my dummy variables are mutually exclusive because they represent crossed experimental conditions in a 2X2 between-subjects factorial design (that also has a within subjects component which I'm not addressing here), so I don't think <code>interaction</code> does what I need to do. </p> <p>For example, my data might look like this:</p> <pre><code>id conditionA conditionB conditionC conditionD 1 NA 1 NA NA 2 1 NA NA NA 3 NA NA 1 NA 4 NA NA NA 1 5 NA 2 NA NA 6 2 NA NA NA 7 NA NA 2 NA 8 NA NA NA 2 </code></pre> <p>I'd like to now make categorical variables that combine ACROSS different types of conditions. For example, people who had values for condition A and B might be coded with one categorical variable, and people who had values for condition C and D.</p> <pre><code>id conditionA conditionB conditionC conditionD factor1 factor2 1 NA 1 NA NA 1 NA 2 1 NA NA NA 1 NA 3 NA NA 1 NA NA 1 4 NA NA NA 1 NA 1 5 NA 2 NA NA 2 NA 6 2 NA NA NA 2 NA 7 NA NA 2 NA NA 2 8 NA NA NA 2 NA 2 </code></pre> <p>Right now, I'm doing this using <code>ifelse()</code> statements, which quite simply is a hot mess (and doesn't always work). Please help! There's probably some super-obvious "easier way."</p> <p>EDIT:</p> <p>The kinds of <code>ifelse</code> commands that I am using are as follows:</p> <pre><code>attach(df) df$factor&lt;-ifelse(conditionA==1 | conditionB==1, 1, NA) df$factor&lt;-ifelse(conditionA==2 | conditionB==2, 2, df$factor) </code></pre> <p>In reality, I'm combining across 6-8 columns each time, so a more elegant solution would help a lot.</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