创建功能以在R中的多个数据范围中对BMI进行分类
这是我在这里的第一篇文章,我对R更新,所以如果这篇文章怪异的话,我深表歉意。
我正在一年对大型数据集进行分析。我想将连续的BMI数据分类为从“体重不足”到“肥胖”的类别。要在此数据集的多年中进行分类,我想编写一个功能,该功能可以在多年内使用数据集略有不同。
有什么方法可以编写此功能,以便在不重写我的代码的情况下将其应用于数据集的不同年份?
bmi_categories<- function(df_bmi_cat){(as.factor(ifelse(df$BMI2< 18.5 &df$AGE2>6, "Underweight",(ifelse (18.5<=df$BMI2 & df$BMI2<25 & df$AGE2>6, "Normal Weight",(ifelse (25<=df$BMI2 & df$BMI2<30 & df$AGE2>6, "Overweight",(ifelse (30<=df$BMI2 & df$AGE2>6, "Obese","")))))))))}
对数据框架的前6个观察结果看起来像:
AGE2 BMI2
1 15 22.50087
2 17 24.88647
3 14 22.70773
4 9 23.49076
5 7 22.14871
6 16 23.10811
事先感谢任何回应的人!
This is my first post here and I'm newer to R so I apologize if this post is worded weird.
I am working on an analysis of a large dataset for a single year. I want to categorize continuous BMI data into the categories ranging from "underweight" to "obese". To categorize across multiple years of this dataset I want to write a function that would be able to be used over multiple years where the datasets are named slightly different.
Is there a way I can write this function so I can apply it to different years of the dataset without rewriting my code??
bmi_categories<- function(df_bmi_cat){(as.factor(ifelse(df$BMI2< 18.5 &df$AGE2>6, "Underweight",(ifelse (18.5<=df$BMI2 & df$BMI2<25 & df$AGE2>6, "Normal Weight",(ifelse (25<=df$BMI2 & df$BMI2<30 & df$AGE2>6, "Overweight",(ifelse (30<=df$BMI2 & df$AGE2>6, "Obese","")))))))))}
The first 6 observations of the dataframe look like this:
AGE2 BMI2
1 15 22.50087
2 17 24.88647
3 14 22.70773
4 9 23.49076
5 7 22.14871
6 16 23.10811
Thanks in advance to anyone who responds!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该做您想要的事情,但嵌套功能少得多。
这将为重量分类创建一个额外的列。
如果需要,这也很容易作为功能应用。
This should do what you want but with a lot less nested functions.
This will create an additional column for the weight classification.
This is also very easy to apply as a function if required.
由于列的名称每次都不同,因此我将不提供整个数据框,而是提供特定数据列。
示例数据
函数
(您也可以使用Jareds使用代码并将其转换为一个函数。我个人尝试在可能的情况下避免使用外部库,因此代码更易于在另一台计算机上运行。)
呼叫功能并将返回值分配给新列
Since the names of the columns are different each time, I would provide the function not with the entire dataframe, but with the specific data columns.
example data
function
(You could also use the code by JaredS and turn that into a function. I personally try to avoid using external libraries where possible, so the code is easier to run on another computer.)
call the function and assign return value to new column