我是R的新手,并使用别人写的脚本。 Belows是脚本的一部分,我收到错误消息:
[.data.frame
中的错误(this.df.merged,c(" MMWR_week"," Syndrome; 访问",:选择了未定义的列
我查找了其他类似问题的回复,但无法找到我的解决方案。任何帮助将不胜感激。
if (!("other" %in% colnames(this.df.merged))) {
this.df.merged$other<-0
}
this.df.merged.by_week<-aggregate(this.df.merged[c("Syndrome Visits","E","other","0-4","25-49","5-24","50-64","over 64","F","M","U","Admit","Disch")],by=list(this.df.merged$MMWR_week),sum)
colnames(this.df.merged.by_week)<-c("MMWR_week","Syndrome Visits","E","other","0-4","25-49","5-24","50-64","over 64","F","M","U","Admit","Disch")
write.csv(this.df.merged.by_week, file=paste(this.diseaseName,"_Count_Query_Summary_Table_by_Week_",this.beginDate,"_",this.endDate,".csv",sep=""))
return(this.df.merged)
}
答案 0 :(得分:0)
这是因为this.df.merged
可能不包含向量c("Syndrome Visits","E","other","0-4","25-49","5-24","50-64","over 64","F","M","U","Admit","Disch")
我建议在this.df.merged中检查名称是否存在和/或拼写正确。以下是一些示例代码,用于检查名称是否存在。您可以自行决定它们为什么不存在,在此之前代码中的其他地方会发生这种情况
if (!("other" %in% colnames(this.df.merged))) {
this.df.merged$other<-0
}
namevector=c("Syndrome Visits","E","other","0-4","25-49","5-24","50-64","over 64","F","M","U","Admit","Disch")
n=colnames(this.df.merged)
tmp=sapply(namevector,function(x){
if(!any(n==x)){
print(paste(x,"not found"))
return(TRUE)
}
return(FALSE)
})
if(any(tmp))stop("some column names not found")
this.df.merged.by_week<-aggregate(this.df.merged[c("Syndrome Visits","E","other","0-4","25-49","5-24","50-64","over 64","F","M","U","Admit","Disch")],by=list(this.df.merged$MMWR_week),sum)
colnames(this.df.merged.by_week)<-c("MMWR_week","Syndrome Visits","E","other","0-4","25-49","5-24","50-64","over 64","F","M","U","Admit","Disch")
write.csv(this.df.merged.by_week,file=paste(this.diseaseName,"_Count_Query_Summary_Table_by_Week_",this.beginDate,"_",this.endDate,".csv",sep=""))
return(this.df.merged)