Danib90 - 1 year ago 130

R Question

I hope this question isn't off topic.

I know how to code a dummy variable in R, however, I was wondering if I could create it in excel. Lets say I have 3 colors (red, blue, yellow) list under a color variable. R would import this as a factor with 3 levels.

Now if I wanted to do this in excel could I make 3 new predictors (instead of color they now become red, blue, and yellow) and place a 1 in the red column if it is red and 0 otherwise and so on? Or will R continue to interpret this as 3 individual factors with 2 levels each?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

So you are manually creating three dummy columns in excel, and want to import them into R? If you later import these columns as numeric rather than factor, there will be no problem.

Well, I still have to remind you that R can code factor to dummy variables, via `model.matrix()`

. So there is never the need to do this thing yourself. It is definitely OK to use a single column with "red", "blue" and "yellow" in excel, and export it into R as factor.

```
colour <- gl(3,2,labels=c("red","blue","yellow"))
model.matrix(~ colour - 1)
# colourred colourblue colouryellow
#1 1 0 0
#2 1 0 0
#3 0 1 0
#4 0 1 0
#5 0 0 1
#6 0 0 1
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**