0

I want to create dummy variables for 25 independent variables in my model. Below is an example how my dataset looks.Y is my dependent variable and Var1, Var2, Var3 are independent variables.

My output should have reference variable for Var1 as A1, Var2 as N1 and Var3 as P1.

Dataset:

Y  Var1        Var2        Var3
1   A1          N3          P4
0   A1          N2          P3
0   A2          N2          P4
1   A1          N1          P2
0   A1          N2          P1

Desired output:

Y  Var1.A2  Var2.N2   Var2.N3  Var3.P2  Var3.P3  Var4.P4    
1    0          0        1       0         0        1
0    0          1        0       0         1        0
0    1          1        0       0         0        0
1    0          0        0       1         0        0
0    0          1        0       0         0        0

Can we run this with using loop as we have 3 indep. variables? It will be very helpful, if any one guide me with the R code?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Ronak Kumar
  • 69
  • 1
  • 1
  • 7
  • 2
    Try `model.matrix( ~ ., df)` – David Arenburg Jan 13 '16 at 19:56
  • Adding to @DavidArenburg's helpful suggestion, `model.matrix( ~.-1, df)` will remove the intercept from the matrix. – bouncyball Jan 13 '16 at 20:01
  • Thanks @DavidArenburg for the code. I have already tried model.matrix() but it fails to create A1 as reference variable for Var1. It creates dummy variables as Var1A1 and VarA2 both. – Ronak Kumar Jan 13 '16 at 20:14
  • `A1` isn't present in your desired output. `model.matrix( ~., df)` gives you *exactly* what you asked for. – David Arenburg Jan 13 '16 at 20:17
  • Yes, i agree. A1 is not present in my desired output. But after running model.matrix(~. -1,df) , it creates dummy variable for both A1 and A2. It should only create dummy variable for A2 keeping A1 as reference variable. – Ronak Kumar Jan 13 '16 at 20:21
  • So run `model.matrix( ~., df)`. You can choose whatever works better for you. – David Arenburg Jan 13 '16 at 20:26

0 Answers0