Categorize Factors

Hi,

I am quite new to R. I've been looking online for a solution, but couldn't find one.
I have an ordinal score reaching from 0-25, which I have categorized as factor().

But I need to categorize the score into "no changes" (0-5 points), "mild changes" (6-10) and "severe changes" (>= 11).

I think I cant use the cut() as it is not a numeric variable.

Any ideas how I can solve this problem?

Thanks!

I believe there are many ways to do this.
Here is just one :


( starting_factor <- factor(0:25,ordered = TRUE) ) 

library(forcats)

fct_collapse(starting_factor,
                      "no changes" = as.character(c(0:5)),
                      "mild changes" = as.character(c(6:10)),
                      "severe changes"=as.character(c(11:25)))

And here's another:

( starting_factor <- factor(0:25,ordered = TRUE) ) 
#>  [1] 0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#> [26] 25
#> 26 Levels: 0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 < 12 < 13 < ... < 25

# Categorize the score into "no changes" (0-5 points), "mild changes" (6-10) and "severe changes" (>= 11).
scorecat <- cut(as.numeric(as.character(starting_factor)), c(-1, 5, 10, 25), labels=c("no change", "mild changes", "severe changes"))

table(starting_factor, scorecat)
#>                scorecat
#> starting_factor no change mild changes severe changes
#>              0          1            0              0
#>              1          1            0              0
#>              2          1            0              0
#>              3          1            0              0
#>              4          1            0              0
#>              5          1            0              0
#>              6          0            1              0
#>              7          0            1              0
#>              8          0            1              0
#>              9          0            1              0
#>              10         0            1              0
#>              11         0            0              1
#>              12         0            0              1
#>              13         0            0              1
#>              14         0            0              1
#>              15         0            0              1
#>              16         0            0              1
#>              17         0            0              1
#>              18         0            0              1
#>              19         0            0              1
#>              20         0            0              1
#>              21         0            0              1
#>              22         0            0              1
#>              23         0            0              1
#>              24         0            0              1
#>              25         0            0              1

Created on 2021-08-03 by the reprex package (v2.0.0)

Awesome! Thanks a lot to both of you!!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.