This is my df:
df <- data.frame(id=as.integer(c(1:6)),
                 code=as.character(c("C410", "D486", "D485", "D501", "D600", "D899")))
df
  id code
1  1 C410
2  2 D486
3  3 D485
4  4 D501
5  5 D600
6  6 D899

I want to attribute causes to each id depending on the range they fall into in column 2. For this, I use a ifelse statement:
    df$cause <- ifelse(df$code >= "C00" & df$code <= "D48", "cause 1", 
                       ifelse(df$code >= "D50" & df$code <= "D89", "cause 2", NA)) 

Issue: the algorithm does not capture values above the end of each range (until the maximum possible value)
df
  id code   cause
1  1 C410 cause 1
2  2 D486    <NA>
3  3 D485    <NA>
4  4 D501 cause 2
5  5 D600 cause 2
6  6 D899    <NA>

Desired output:
 df
      id code   cause
    1  1 C410 cause 1
    2  2 D486 cause 1
    3  3 D485 cause 1  
    4  4 D501 cause 2
    5  5 D600 cause 2
    6  6 D899 cause 2


Retrieve every value between an alphanumeric range in R using ifelse
Yo need to add a third digit:
df$cause <- ifelse(df$code >= "C000" & df$code <= "D489", "cause 1", 
                   ifelse(df$code >= "D500" & df$code <= "D899", "cause 2", NA))


> df
  id code   cause
1  1 C410 cause 1
2  2 D486 cause 1
3  3 D485 cause 1
4  4 D501 cause 2
5  5 D600 cause 2
6  6 D899 cause 2

