I learning the basics of R and i'm trying to group a column according to a number of ranges. my column is BMI and i want to group the values in it such as underweight, healthy, overweight and obese. Can i please know how to do it? my data frame is weight_log.
Asked
Active
Viewed 383 times
0
-
Welcome - what did you try so far? Please read [this post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to ask a good question on R. In particular, please edit your question to include the ouput of ``dput(data)`` and your desired output. Thank you. – user438383 Jun 20 '21 at 08:20
1 Answers
0
There are lots of ways to achieve your goal using R, but you are encouraged to show your own attempts to solve the problem when you post questions on StackOverflow to help us troubleshoot the issue/s.
Here is a similar example using tidyverse functions that might help you get started:
# Load libraries
library(tidyverse)
# Generate some fake data for the example
subjects <- data.frame(height = rnorm(100, 1.6, 0.2),
weight = rnorm(100, 75, 20))
# Calculate BMI and categorise subjects (per wikipedia)
subjects %>%
mutate(BMI = weight / (height^2)) %>%
mutate(`BMI category` = case_when(
BMI < 15 ~ "Very severely underweight",
BMI >= 15 & BMI < 16 ~ "Severely underweight",
BMI >= 16 & BMI < 18.5 ~ "Underweight",
BMI >= 18.5 & BMI < 25 ~ "Normal",
BMI >= 25 & BMI < 30 ~ "Overweight",
BMI >= 30 & BMI < 35 ~ "Moderately obese",
BMI >= 35 & BMI < 40 ~ "Severely obese",
BMI >= 40 ~ "Very severely obese")
)
# height weight BMI BMI category
#1 1.600551 90.82957 35.45588 Severely obese
#2 1.673910 90.08658 32.15111 Moderately obese
#3 1.284048 47.01420 28.51456 Overweight
#4 1.474780 113.51028 52.18918 Very severely obese
#5 1.778946 90.02104 28.44581 Overweight
#6 1.353927 65.38778 35.67025 Severely obese
#7 1.492418 75.08285 33.71010 Moderately obese
#8 1.567819 51.66703 21.01947 Normal
...

jared_mamrot
- 22,354
- 4
- 21
- 46