Created
October 13, 2025 10:07
-
-
Save thanhyds1998/62ecf8872b6f4e7c913e5c4287aaa201 to your computer and use it in GitHub Desktop.
Create new categorical variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Method 1: case_when() | |
umaru = umaru %>% mutate(depression = case_when(BECK < 10 ~ 1, | |
BECK < 19 ~ 2, | |
BECK < 30 ~ 3, | |
BECK >= 30 ~ 4)) | |
Method 2: Avec case_when() + between() (lisible) | |
library(dplyr) | |
umaru <- umaru %>% | |
mutate( | |
depression = case_when( | |
dplyr::between(BECK, 0, 9) ~ "Pas/minimale", | |
dplyr::between(BECK, 10, 18) ~ "Légère", | |
dplyr::between(BECK, 19, 29) ~ "Modérée", | |
BECK >= 30 ~ "Sévère", | |
TRUE ~ NA_character_ | |
), | |
depression = factor(depression, | |
levels = c("Pas/minimale", "Légère", "Modérée", "Sévère"), | |
ordered = TRUE) | |
) | |
) | |
# Method 3: cut() | |
library(dplyr) | |
umaru <- umaru %>% | |
mutate( | |
depression = cut( | |
BECK, | |
breaks = c(-Inf, 9, 18, 29, Inf), # 0–9 ; 10–18 ; 19–29 ; 30+ | |
labels = c("Pas/minimale", "Légère", "Modérée", "Sévère"), | |
right = TRUE, # intervalles (a, b] → 9 dans 1ère cat., 10 dans 2e, etc. | |
include.lowest = TRUE | |
) | |
) | |
# Methode 4: Base R uniquement | |
umaru$depression <- cut( | |
umaru$BECK, | |
breaks = c(-Inf, 9, 18, 29, Inf), | |
labels = c("Pas/minimale", "Légère", "Modérée", "Sévère"), | |
right = TRUE, include.lowest = TRUE | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment