我的任务是创建一个循环来完成一些事情:
基本上,目标是从每个患者的一组观察中获得第一个非缺失高度值。每个患者都有不同的观察量和“缺失”,因此建议我为此任务创建一个循环
这是我的尝试:
for (i in 1:length(patients)){
# select data for one patient
patient = patients[i]
SubjectID_patient = filter(DF, Subject.ID == patient)
#select data which is not missing
complete_hts=filter(SubjectID_patient,
!is.na(SubjectID_patient$height_cm))
#select first visit for height
complete_hts=complete_hts[!duplicated(complete_hts$Subject.ID),]
# if there are 0 entries remaining, return NAs
????
#make dataframe for the patient
df_patient = data.frame(Subject.ID = complete_hts$Subject.ID,
height = complete_hts$height_cm)
##bind rows of this dataframe to first dataframe
baseline_new = rbind(baseline_new, df_patient)
}
正如您所看到的,我被困在那个部分,如果特定患者的“高度”变量有0个条目,则返回NA。循环工作正常,直到遇到有0个有效值的访问,因此我无法在最后创建数据帧。任何建议将不胜感激。请原谅我的不正确格式(如果有的话),因为这是我在stackoverflow上的第一篇文章!
谢谢!
答案 0 :(得分:0)
如果SubjectID_patient $ height_cm中只有NA,那么complete_hts应为空data.frame和nrow(complete_hts)应该等于0.您可以使用if语句:
if (nrow(complete_hts)!=0) {
#select first visit for height
complete_hts=complete_hts[!duplicated(complete_hts$Subject.ID),]
} else {
complete_hts$height_cm=NA
}
#make dataframe for the patient
df_patient = data.frame(Subject.ID = complete_hts$Subject.ID,
height = complete_hts$height_cm)
##bind rows of this dataframe to first dataframe
baseline_new = rbind(baseline_new, df_patient)
}