Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ty this</p> <pre><code>set.seed(001) # generating some data sex &lt;- factor(sample(1:2, 10, replace=TRUE)) # this is what you have [1] 1 1 2 2 1 2 2 2 2 1 Levels: 1 2 sex&lt;-factor(ifelse(as.numeric(sex)==2, 1,0)) # this is what you want sex [1] 0 0 1 1 0 1 1 1 1 0 Levels: 0 1 </code></pre> <p>If you want labels to be 0 = Male and 1 = Female, then...</p> <pre><code>sex&lt;-factor(ifelse(as.numeric(sex)==2, 1,0), labels=c('M', 'F')) sex # this is what you want [1] M M F F M F F F F M Levels: M F </code></pre> <p>Actually you don't need to create a dummy variable in order to estimate a model using <code>lm</code>, let's see this example:</p> <pre><code>set.seed(001) # Generating some data N &lt;- 100 x &lt;- rnorm(N, 50, 20) y &lt;- 20 + 3.5*x + rnorm(N) sex &lt;- factor(sample(1:2, N, replace=TRUE)) # Estimating the linear model lm(y ~ x + sex) # using the first category as the baseline (this means sex==1) Call: lm(formula = y ~ x + sex) Coefficients: (Intercept) x sex2 19.97815 3.49994 -0.02719 # renaming the categories and labelling them sex&lt;-factor(ifelse(as.numeric(sex)==2, 1,0), labels=c('M', 'F')) lm(y ~ x + sex) # the same results, baseline is 'Male' Call: lm(formula = y ~ x + sex) Coefficients: (Intercept) x sexF 19.97815 3.49994 -0.02719 </code></pre> <p>As you can see R deals with the dummies pretty well, you just pass them into the formula as <code>factor</code> variable and R will do the rest for you.</p> <p>By the way there's no need to change the categories from c(2,1) into c(0,1), the results will be the same as you can seen in the example above. </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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