0

I am very new to R and using a script someone else wrote. Belows is part of the script where I am getting an error message:

Error in [.data.frame(this.df.merged, c("MMWR_week", "Syndrome Visits", : undefined columns selected

I looked up other repsonses to similar questions and couldn't really find my solution. Any help would be greatly appreciated.

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)

}

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
Enrique
  • 1
  • 1
  • 1
  • 1
    pleasemakeyourcodemorereadable – Rich Scriven Oct 17 '14 at 18:54
  • Seems unlikely there would be a column names "Syndrome Visits" (with a space in it). What does `names(this.df.merged)`. When posting a question, please take the time to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) because otherwise we have no idea what the structures of the objects you are working with are which makes it very difficult to help you. – MrFlick Oct 17 '14 at 19:13
  • Thanks Richard, still trying to get the hang of this. Your suggestion seems to be correct, I don't think syndrome visits exists. I will look into this more, thanks for the comment. – Enrique Oct 17 '14 at 19:25

1 Answers1

0

This happens because this.df.merged may not contain one of the names in the vector c("Syndrome Visits","E","other","0-4","25-49","5-24","50-64","over 64","F","M","U","Admit","Disch")

I suggest checking that the names do exists and/or are spelled correctly in this.df.merged. Here is some sample code to check that the names exist. It is up to you to decide why they don't exist, which would have occurred somewhere else in your code before this point

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)
Oscar
  • 855
  • 9
  • 13
  • Thanks Oscar, you are right, the column for "Syndrome Visits" was never created for some reason. The first column created was "E" and so on. I will look into this. – Enrique Oct 17 '14 at 19:48