我想创建一个新列,将一个人的BMI分为某些类别。
我不确定我要去哪里哪里
brfss2013 <- brfss2013 %>%
mutate(bmi_class = if (X_bmi5 < 18.5) {
X_bmi5 == 'underweight'}
else if (X_bmi5 in range(18.5,24.9)){
X_bmi5 =='normal'}
else if (X_bmi5 in range(25,29.9)) {
X_bmi5 =='overweight'}
else if (X_bmi5 in range(30,34.9)){
X_bmi5 =='class 1 obesity'}
else if (X_bmi5 in range(35,39.9)){
X_bmi5 =='class 2 obesity'
else if (X_bmi5 > 39.9){
X_bmi5 == 'class 3 obesity')}
else 'NA')
答案 0 :(得分:3)
@joran已经提到了cut
函数。所以这是代码:
# Fake data
brfss2013 <- data.frame(X_bmi5 = rnorm(30, 28, 5))
labels <- c('underweight', 'normal', 'overweight',
'class 1 obesity', 'class 2 obesity', 'class 3 obesity')
breaks <- c(0, 18.5, 24.9, 29.9, 34.9, 39.9, 1000)
brfss2013 <- brfss2013 %>%
mutate(brfss2013 = cut(X_bmi5, breaks = breaks,
labels = labels, include.lowest = TRUE))