Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I take multiple vectors and recode their datatypes in R?
    primarykey
    data
    text
    <h3>I'm looking for an elegant way to change multiple vectors' datatypes in R.</h3> <p>I'm working with an educational dataset: 426 students' answers to eight multiple choice questions (<code>1</code> = correct, <code>0</code> = incorrect), plus a column indicating which instructor (<code>1, 2, or 3</code>) taught their course.</p> <p>As it stands, my data is sitting pretty in <code>data.df</code>, like this:</p> <p><pre><code> str(data.df) 'data.frame': 426 obs. of 9 variables: $ ques01: int 1 1 1 1 1 1 0 0 0 1 ... $ ques02: int 0 0 1 1 1 1 1 1 1 1 ... $ ques03: int 0 0 1 1 0 0 1 1 0 1 ... $ ques04: int 1 0 1 1 1 1 1 1 1 1 ... $ ques05: int 0 0 0 0 1 0 0 0 0 0 ... $ ques06: int 1 0 1 1 0 1 1 1 1 1 ... $ ques07: int 0 0 1 1 0 1 1 0 0 1 ... $ ques08: int 0 0 1 1 1 0 1 1 0 1 ... $ inst : num 1 1 1 1 1 1 1 1 1 1 ... </code></pre></p></p> <p>But those <code>ques0x</code> values aren't <strong>really</strong> integers. Rather, I think it's better to have R treat them as experimental factors. Same goes for the "inst" values.</p> <h3>I'd love to turn all those <code>int</code>s and <code>num</code>s into <code>factors</code> </h3> <p> Ideally, an elegant solution should produce a dataframe&mdash;I call it <code>factorData.df</code>&mdash;that looks like this: </p> <pre><code> str(factorData.df) 'data.frame': 426 obs. of 9 variables: $ ques01: Factor w/ 2 levels "0","1": 2 2 2 2 2 2 1 1 1 2 ... $ ques02: Factor w/ 2 levels "0","1": 1 1 2 2 2 2 2 2 2 2 ... $ ques03: Factor w/ 2 levels "0","1": 1 1 2 2 1 1 2 2 1 2 ... $ ques04: Factor w/ 2 levels "0","1": 2 1 2 2 2 2 2 2 2 2 ... $ ques05: Factor w/ 2 levels "0","1": 1 1 1 1 2 1 1 1 1 1 ... $ ques06: Factor w/ 2 levels "0","1": 2 1 2 2 1 2 2 2 2 2 ... $ ques07: Factor w/ 2 levels "0","1": 1 1 2 2 1 2 2 1 1 2 ... $ ques08: Factor w/ 2 levels "0","1": 1 1 2 2 2 1 2 2 1 2 ... $ inst : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ... </code></pre> <p><p>I'm fairly certain that whatever solution you folks come up with, it ought to be easy to generalize to any n number of variables that'd need to get reclassified, and would work across most common conversions (<code>int -> factor</code> and <code>num -> int</code>, for example).</p> <h3> No matter what solution you folks generate, it's <strong>bound</strong> to be more elegant than mine</h3> <p><p>Because my current clunky code is just 9 separate <code>factor()</code> statements, one for each variable, like this</p> <pre> factorData.df$ques01 </pre> <p> I'm brand-new to R, programming, and stackoverflow. Please be gentle, and thanks in advance for your help! </p>
    singulars
    1. This table or related slice is empty.
    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