In my database, I have a Variable TP_CorRaca with values 0,1,2,3,4,5. Each number corresponds to a color.
Example: 0 - White 1 - Black 2-brown ....
I want to make this match to use the lm ()
In my database, I have a Variable TP_CorRaca with values 0,1,2,3,4,5. Each number corresponds to a color.
Example: 0 - White 1 - Black 2-brown ....
I want to make this match to use the lm ()
This is a case where the factors are useful. Just make your variable a factor.
Creating an example vector:
set.seed(10)
exemplo_numero <- sample(0:5, 10, replace = TRUE)
You can use the factor()
function explaining what each number is:
exemplo_fator <- factor(exemplo_numero,
levels = 0:5,
labels = c("branco", "preto", "pardo", "amarelo", "indigena", "não declarado"))
exemplo_fator
[1] amarelo preto pardo indigena branco preto preto preto amarelo pardo
Levels: branco preto pardo amarelo indigena não declarado
It has the function recode
of dplyr
.
Using the same example of @Carlos Cinelli
set.seed(10)
exemplo_numero <- sample(0:5, 10, replace = TRUE)
library(dplyr)
recode(exemplo_numero, '0' = "branco", '1' = "preto",'2'= "pardo",
'3' = "amarelo", '4' = "indigena", '5' = "não declarado")
#> [1] "amarelo" "preto" "pardo" "indigena" "branco" "preto" "preto" "preto" "amarelo" "pardo"
Note that the numbers must be between "" the same as the crase accent.
The legal of the recode
function are the other parameters such as .default
and .missing
.
x <- c(1:4, NA)
recode(x, '1' = "z", '2' = "y", .default = "D")
#> [1] "z" "y" "D" "D" NA
recode(x, '1' = "z", '2' = "y", .default = "D", .missing = "M")
#> [1] "z" "y" "D" "D" "M"
The .default
can be used to populate the values not mentioned in the code by some value. The .missing
substitutes value NA
by some value as well.