0

I am currently looking at some data that I am using for a logit. I have the logit working just fine, however, I am being asked to break out the data into deciles and then potentially do things with those. To that end, I imagine I need to turn the deciles I create into variables (e.g., decile 1, decile 2...). My question is, how can I turn the deciles I create into variables that I can then do something with?

I've tried Googling but I can only get how to break things down into quantiles using quantile(x, probs = seq (0,1,1/10)) function and also using dplyr. I can get the deciles just fine, but I can't find anything on how to turn those into anything useful.

Thanks in advance!

Sotos
  • 51,121
  • 6
  • 32
  • 66
StMatthias
  • 19
  • 5
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 13 '23 at 15:31
  • 1
    Wow ... 6 minutes in and there are already two close votes. I suggest you heed MrFlick's advise to make this reproducible, and you might opt to delete it, edit it for content, then undelete it to preclude somebody from deleting it out from under you (it's one technique, not required). – r2evans Feb 13 '23 at 15:33
  • 2
    It's not clear what about `quantile(x, probs = seq (0,1,1/10))` isn't sufficient---if you add that as a column to your data, you can do a lot with it! What "useful" things are you trying to do, and how are you trying to do them? – Gregor Thomas Feb 13 '23 at 16:18
  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Community Feb 13 '23 at 18:01

1 Answers1

1

ntile will give you most of what you want, but it will return a numeric result.

I gather you would like to use the decile as a categorical variable in a model, so you might want to create a factor varaible instead.

library(tidyverse)

x <- runif(30)

xf <- x %>%
  ntile(10) %>%
  paste0("decile", .) %>%
  as.factor() %>%
  reorder(parse_number(as.character(.)))

print(xf)
#>  [1] decile4  decile3  decile6  decile9  decile7  decile5  decile7  decile10
#>  [9] decile1  decile3  decile2  decile4  decile8  decile10 decile10 decile9 
#> [17] decile6  decile2  decile6  decile8  decile5  decile4  decile9  decile5 
#> [25] decile8  decile7  decile2  decile1  decile1  decile3 
#> attr(,"scores")
#>  decile1 decile10  decile2  decile3  decile4  decile5  decile6  decile7 
#>        1       10        2        3        4        5        6        7 
#>  decile8  decile9 
#>        8        9 
#> 10 Levels: decile1 decile2 decile3 decile4 decile5 decile6 decile7 ... decile10

Created on 2023-02-13 with reprex v2.0.2

Arthur
  • 1,248
  • 8
  • 14