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

Asked

Viewed 186 times

4

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

2 answers

4

Another option is to use the function cut. 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 numerical vector, the second is the cut vector, the third is to indicate whether it includes the lower value, 0, and last are the categories you want.

  • The advantage here is that in this the column type categorias will be Factor instead of Character. Factors are more economical in terms of memory.

  • You can also add the ordered_result=TRUE argument to sort the categories, depending on the analysis you want to do.

3


A simple and easy to understand way is by using the logical vectors of data analysis. Vectors generate TRUE/FALSE values that validate the next execution. In this example, the data is in a 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
  • Thanks, I tried to do "e" with "&" but I was doing it wrong. :)

Browser other questions tagged

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