Problems coding a variable in R

0

I want to code a variable in 4 categories. I know that I should use the function cut , but for many examples that I see I do not understand it.

The variable should encode it into 4 categories:

  • The first category would be between its minimum and the first quartile.
  • The second between the first quartile and the second.
  • The third between the second quartile and the third quartile.
  • The fourth between the third quartile and the fourth quartile.

All categories with intervals of the form [A,B] minus the first that will be [Minimo,Q1] .

I do not know how to do it. For two days I have been searching, remixing and apart I do not see similar examples.

    
asked by Willy 15.10.2016 в 11:21
source

1 answer

1

You can use the option include.lowest of the function cut ,

> datos = seq(1, 10)
> datos
 [1]  1  2  3  4  5  6  7  8  9 10

> categorías = cut(datos, breaks=quantile(datos), include.lowest=TRUE)
> categorías
 [1] [1,3.25]   [1,3.25]   [1,3.25]   (3.25,5.5] (3.25,5.5] (5.5,7.75]
 [7] (5.5,7.75] (7.75,10]  (7.75,10]  (7.75,10] 
Levels: [1,3.25] (3.25,5.5] (5.5,7.75] (7.75,10]

> table(categorías)
categorías
  [1,3.25] (3.25,5.5] (5.5,7.75]  (7.75,10] 
         3          2          2          3 

quantile , generates the minimum values. maximum and quartiles.

    
answered by 15.10.2016 в 17:05