2014-10-17 61 views
0

我對R很陌生,並且使用別人寫的腳本。初級講座在這裏我得到一個錯誤信息的腳本的一部分:未定義列選擇R中的錯誤消息

錯誤[.data.frame(this.df.merged,C( 「MMWR_week」, 「證 訪問」:選擇未定義列

我擡頭其他repsonses類似的問題,並不能真正找到我的解決方案。任何幫助,將不勝感激。

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) 

}

+1

pleasemakeyourcodemorereadable – 2014-10-17 18:54:50

+0

似乎不太可能會有一個列名「綜合症訪問」(中間有空格前在裏面)。 'name(this.df.merged)'是什麼。當發佈一個問題時,請花時間包含一個[可重現的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),否則我們沒有想法你正在使用的物體的結構是什麼,這使得很難幫助你。 – MrFlick 2014-10-17 19:13:53

+0

感謝理查德,仍然試圖得到這個竅門。你的建議似乎是正確的,我不認爲存在綜合徵。我會更多地考慮這一點,感謝評論。 – Enrique 2014-10-17 19:25:33

回答

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) 
+0

謝謝奧斯卡,你是對的,「綜合徵訪問」這個專欄從來沒有出於某種原因創建。第一列創建爲「E」等。我會研究這一點。 – Enrique 2014-10-17 19:48:06