1
I would like to create Dummies to identify the company in the database. For example, a new variable called "GLO" would be 1 if the enterprise variable assumed the value GLO and 0 c.c.
The data structure is like this:
head(tarifas)
ano mes empresa origem destino tarifa assentos
1 2002 1 GLO SBPA SBBR 397,00 51
2 2002 1 GLO SBSV SBRF 272,00 5
3 2002 1 GLO SBFL SBGL 223,00 196
4 2002 1 GLO SBGL SBSP 96,00 615
5 2002 1 GLO SBGL SBRF 340,00 297
6 2002 1 GLO SBSP SBFL 145,00 189
What I tried to do was use the dplyr package along with for loop, but something is wrong. For example, to create an identifier for the company GLO and AZU, I used the following code:
for (k in c("GLO", "AZU")) {
tarifas2<- tarifas %>%
mutate(paste0(k) = 0) %>%
mutate(replace(paste0(k), empresa == paste0(",k,"),1))
}
Try
model.matrix(~ 0 + empresa, df1)
. But note that it is almost certain not to need to create Dummies explicitly, the R modeling functions do this automatically.– Rui Barradas