【发布时间】:2020-03-26 23:02:09
【问题描述】:
因此,对于数据集 birthwt,我想要从吸烟且出生时未满 20 岁的母亲所生的低体重婴儿的百分比。换句话说,我想要年龄
我运行接下来的三段代码,实际上给出了正确的答案:
# new df with the conditions
new_df <- subset(birthwt, age<20 & smoke==1)
#for loop to calculate the low weight
low_weight <- 0
for (i in 1:length(new_df$bwt)){
if(bwt[i] < 2600){
low_weight <- low_weight + 1
}
}
#low weight for the original dataset
low_weight_tot <- 0
attach(birthwt)
for (i in 1:length(birthwt$bwt)){
if(bwt[i] < 2600){
low_weight_tot <- low_weight_tot + 1
}
}
print(low_weight/low_weight_tot)*100
但是我觉得这很乏味,有没有其他更简单的方法可以用循环来做到这一点?
谢谢!
【问题讨论】:
-
有什么乏味的?
-
而且它需要是一个循环?
-
这个百分比的分母是什么:所有婴儿还是只有低出生体重 (
-
它在实际中不需要是一个循环,但它确实是为了练习的目的。分母将是出生婴儿的总数。感谢您的回答!