codes for grouping a continuous variable to a categorical variable

Hello,

Could someone show me the codes for grouping a continuous variable as a categorical variable? for instance, BMI in my dataset is a continuous variable, if I want to redefine the variable in a new variable bmigp as
bmigp = 1 if bmi<25
bmigp= 2 if 25<= bmi< 30
bmigp= 3 if bmi>=30

what should the codes be?

Thanks in advance!

Jason

Hi @Jason1,

Take a look at this:

library("tidyverse")
my_data <- tibble(bmi = runif(n = 100,
                              min = 20,
                              max = 40))
my_data_aug <- my_data %>% 
  mutate(bmigp = case_when(bmi < 25 ~ 1,
                           25 <= bmi & bmi < 30 ~ 2,
                           30 <= bmi ~ 3))

Hope it helps :slightly_smiling_face:

1 Like

Thanks Leon. it works. BTW, I found another way that does the same job before seeing yours. ie,
mutate(bmigp=cut(bmi, c(0, 25, 30, 40), labels=c("nwt", "owt", "obese"), LEFT =FALSE)))

Jason

Hi Leon,
Thanks for your kindness help. I'm a SAS user and try to learn R now. I know how to do data merge in SAS, but have trouble doing so in R. I have two datasets A and B, they have a variable ID that can be used for merging. I try to add information of 3 variables from data B, ie, x, y, and z to data A. what should be the codes? I appreciate your help in advance.
Jason

You'll need to put in some effort, but your R-journey will be well worth it. What you're asking about is called Relational Data and I would recommend going through the corresponding chapter in R for Data Science. Moreover, if you really want to get into modern R - I HIGHLY recommend you going through R4DS back-to-back :+1:

Kindly remember to mark the solution :+1:

1 Like

Thanks Leon, I'm half way to get chapter 13. I try to practice with some of my own work, but had trouble.
Jason

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