0

So I have health data which I am analysing and I want to add a new column which categorises BMI into underweight, normal weight, and overweight. Below is a sample if the data. The BMI variable is a column.

bmi 
21.9671
34.9279
23.507
17.679
43.678

what I would like is to make a column called rating which will take values of bmi <= 18.5 and give a rating of underweight, if the bmi is between 18.5 and 25 then rating should be normal weight otherwise rating will be overweight. What I want the rating column to look like according to the bmi given above is given below.

rating normal weight overweight' normal weight underweight
overweight.

I tried an if statement but it only takes the first row and applies to the rest of the columns. SO it will give that all BMIs belong in the category "normal weight"

I would really appreciate the help

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Tino
  • 1
  • 1

1 Answers1

0

The case_when function from the dplyr library is well suited to your requirement:

df$rating <- case_when(
    df$bmi <= 18.5 ~ "underweight",
    df$bmi <= 25 ~ "normal",
    TRUE ~ "overweight"
)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thank you Tim, that is very helpful, onward with my final project I go. I really appreciate the help. – Tino Dec 10 '20 at 04:49