且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尝试使用R中的多个if else语句创建新列

更新时间:2023-12-01 11:36:04

@joran已经提到了 cut 函数.所以这是代码:

@joran already mentioned cut function. So here is the code:

# 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))