0

the information is loaded from a .csv file and is extensive

x=loan$Debt.To.Income.Ratio
x=factor(c("0.00-0.10","0.11-0.30", ">0.31"),
           levels=c("Low", "Medium", "High")
        )
table(x)

output

Low Medium High 0 0 0

for some reason none of the values are being categorized into the Low Medium and High levels.

WAF
  • 1,141
  • 20
  • 44
  • Can you provide a reprex? Even if you just use `dput(head(df))` it will be helpful – Matt Apr 01 '20 at 02:12
  • I edited the question so the code looks like the actual code. Sorry, my first time posting a question. – Barrett Seldon Apr 01 '20 at 03:21
  • Check output of `factor(c("0.00-0.10","0.11-0.30", ">0.31"), levels=c("Low", "Medium", "High"))` it returns all `NA`'s. Why do you think `table` would return any count at all? Please be clear on your input and expected output so that it is easier to help you. You can read on how to give a [reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Apr 01 '20 at 14:49

1 Answers1

0

You can use cut, and specify the breaks argument. I'll use dplyr because it's makes the transformations easy, but you can use base R to make the grouping variable if you want:

library(dplyr)

df <- mtcars %>%
    select(mpg) %>%
    mutate(group = cut(mtcars$mpg, breaks = c(10,15,20,25,30,35)))

head(df, 10)

# A tibble: 10 x 2
#      mpg group  
#    <dbl> <fct>  
#  1  21   (20,25]
#  2  21   (20,25]
#  3  22.8 (20,25]
#  4  21.4 (20,25]
#  5  18.7 (15,20]
#  6  18.1 (15,20]
#  7  14.3 (10,15]
#  8  24.4 (20,25]
#  9  22.8 (20,25]
# 10  19.2 (15,20]

levels(df$group)
# [1] "(10,15]" "(15,20]" "(20,25]" "(25,30]" "(30,35]"
heds1
  • 3,203
  • 2
  • 17
  • 32