When cutting data into intervals, mutate(cut) adds a number not in my label

I searched "cut mutate add label" and did not find a similar post.

I have various columns of lab values and am converting each one up to a score, they all get added up and the total is used to access a patients mortality. One of them, below (reprex with fake data), isn't working and the rest are.

I'm curious as to why a label is present that I did not choose (I have either a 4, 3, 2, 1 or 0) and why it is backwards (5 should be labeled a 4, not a 1).

library(tidyverse)
df<-
tibble::tribble(
               ~plt,
  5, 
  25,
  75,
  125,
  250,
  ) 

df %>%
  mutate(sofa_plt = as.numeric(cut(plt, breaks=c(0,19,49,99,149,1000), include.lowest=TRUE, labels=c("4", "3", "2", "1", "0"), ordered_result = TRUE)))
#> # A tibble: 5 x 2
#>     plt sofa_plt
#>   <dbl>    <dbl>
#> 1     5        1
#> 2    25        2
#> 3    75        3
#> 4   125        4
#> 5   250        5

cut() returns a factor, which begins numbering with 1. To convert a factor to numeric, first convert to character and then numeric. Like so:

> df %>%
+   mutate(sofa_plt = as.numeric(as.character(cut(plt, breaks=c(0,19,49,99,149,1000), 
                      include.lowest=TRUE, 
                      labels=c("4", "3", "2", "1", "0"), 
                      ordered_result = TRUE))))
# A tibble: 5 x 2
    plt sofa_plt
  <dbl>    <dbl>
1     5        4
2    25        3
3    75        2
4   125        1
5   250        0
1 Like

This topic was automatically closed 7 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.