How can I transform a variable (0-10) into 3 categories?

4

The variable GLEASON of the database is 0 to 10. And I wanted to change this variable into 3 categories, for example: 0-4: not very aggressive, 5-7: aggressive intermediate and 8-10: very aggressive. In programming R. Thank you

    
asked by anonymous 13.04.2017 / 10:03

2 answers

3

A simple and easy-to-understand way is to use the logical vectors of data analysis. The vectors generate TRUE / FALSE values that validate the next execution. In this example, the data is in data.frame and when doing the first check we automatically create the variable categoria .

dados <- data.frame(GLEASON = sample(0:10, 50, replace = TRUE))

dados$categorias[dados$GLEASON <= 4] <- 'pouco agressivo'
dados$categorias[dados$GLEASON >= 5 & dados$GLEASON <= 7] <- 'agressivo intermedio'
dados$categorias[dados$GLEASON >= 8] <- 'muito agressivo'

head(dados)
 GLEASON           categorias
       9      muito agressivo
       1      pouco agressivo
       8      muito agressivo
      10      muito agressivo
      10      muito agressivo
       6 agressivo intermedio
    
13.04.2017 / 11:16
4

Another option is to use the cut function. Using the data.frame created by @Daniel:

dados <- data.frame(GLEASON = sample(0:10, 50, replace = TRUE))
dados$categorias <- cut(dados$GLEASON, c(0,4,7,10),
   include.lowest = T, labels = c("pouco agressivo",
   "agressivo intermedio","muito agressivo"))

The first argument is the numeric vector, the second is the vector of cuts, the third is to indicate if it includes the lowest value, 0, and last are the categories you want.

    
13.04.2017 / 12:36