Search code examples
rcutquantilepercentile

Use quantile to get percentile in r


I am trying to classify my data to 3 groups between 30th and 70th percentile, Previosly was easy for me to sahre my data by equal groups like my code below prove.

MV_UK$`1993` <- with(MV_UK, cut(MV_UK$`1993`, breaks = quantile(MV_UK$`1993`, probs = seq(0,1 , by= 0.5), na.rm = TRUE), include.lowest = TRUE , labels= c("S","B") )) 

I tried to this code

  MV_UK$`1992` <- with(MV_UK, cut(BTM_UK$`1992`, breaks = quantile(MV_UK$`1992`, probs = c(.3,.7) , na.rm = TRUE), include.lowest = TRUE , labels= c("L","M","B") )) 

But I get an Error

  lengths of 'breaks' and 'labels' differ

Solution

  • It's basically what the error says - In the second example, you are providing 2 groups (probs = c(.3,.7)), which is fewer than the amount of labels you provide (c("L","M","B"))

    Notice that in your first code snippet, seq(0,1 , by= 0.5) You generate 3 distinct values (0.0, 0.5, 1.0)

    So you can either add another cutoff point, or remove a label.