How to convert numbers into categories in R

Asked

Viewed 800 times

5

In my database, I have a Variable TP_CorRaca with the values 0, 1, 2, 3, 4, 5. Each number corresponds to a color.

Example:

0 - White

1 - black

2 - brown

...

I want to make that match to use the function lm().

2 answers

6

This is a case where factors are useful. Just turn your variable into a factor.

Creating an example vector:

set.seed(10)
exemplo_numero <- sample(0:5, 10, replace = TRUE)

You can use the function factor() 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

2

Has the function recode of dplyr. Using the same example as @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 need to be between "`" the same as the crase accent. The legal of the function recode 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 fill in the values not mentioned in the code for some value. The .missing replaces valore NA for some value too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.