在 for 循环中使用条件的最快方法

Roy*_*tty 2 loops for-loop r

因此,对于数据集birthwt,我想要母亲吸烟且分娩时未满 20 岁的低体重婴儿的百分比。换句话说,我想要 <2600 的 bwt(重量)是 <20 岁且烟雾 == 1。

我运行接下来的三段代码,它们实际上给了你正确的答案:

 # 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
Run Code Online (Sandbox Code Playgroud)

但我觉得它很乏味,有没有其他更简单的方法可以用循环来做到这一点?

谢谢!

eas*_*00d 5

你不需要循环:

library(dplyr)
birthwt %>% 
  summarise(perc = mean(age < 20 & smoke == 1 & bwt < 2600))
Run Code Online (Sandbox Code Playgroud)