undefined columns selected error message in R - r

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)
}

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)

Related

Length Zero in r

Greetings I am getting an error of
Error in if (nrow(pair) == 0) { :argument is of length zero
I have checked the other answers but do not seem to work on a variable like mine. Please check code below, please assist if you can.
pair<-NULL
if(exists("p.doa.ym")) pair <- rbind(pair, p.doa.ym[,1:2])
if(exists("p.doa.yd")) pair <- rbind(pair, p.doa.yd[,1:2])
if(nrow(pair) == 0) {
print("THERE ARE NO MATCHES FOR TODAY. STOP HERE")
quit()
}
Since you set pair=NULL and then it might happen that pair stays null if those two if statements are not true, you either need to check if pair is null first, or you could set pair to an empty data frame, or something else.
One option:
if (!is.null(pair)) {
if (nrow(pair)==0) {
# your code
}
}
Another option:
pair=data.frame()
# your code

Create a series of new folders in for loop in R

I have create a small script that passes a vector through a loop. In this loop I am using an if else statement to check if folder exists and if not to create the folder. However, I am getting error: Error in file.exists(i) : invalid 'file' argument. This has to due with file.exist(). I dont understand why this isnt ok. I check the man using help. Seems like this should be working.
folders<- c("RawData", "Output", "BCV", "DEplots", "DEtables", "PathwayOuts", "VolcanoPLots")
for(i in 1:length(folders)){
if (file.exists(i)){
cat(paste0(i, "already exists"))
} else {
cat(paste0(i, "does not exists"))
dir.create(i)
}
}
You are looping over an index (that is, 1:length(folders) is just the vector 1:7, not the values of the folders vector itself. The easiest solution is to loop over the vector itself:
for (i in folders) {
Or, if you still want to loop over the index:
for (i in 1:length(folders)) {
if (file.exists(folders[i])){
cat(paste0(folders[i], "already exists"))
}
else {
cat(paste0(folders[i], "does not exists"))
dir.create(folders[i])
}
}
A quick tip: if you are debugging a for-loop, the place to start is to add print(i) at the start of the loop. You would have immediately seen the problem: i was an integer, not the first value of the vector.

Error: missing value where True/False

I am trying to delete all values in a list that have the tag ".dsw". My list is a list of files using the function list.files. This is my code:
for (file in GRef) {
if (strsplit(file, "[.]")[[1]][3] == "dsw") {
#GRef=GRef[-file]
for(n in 1:length(GRef)){
if (GRef[n] == file){
GRef=GRef[-n]
}
}
}
}
Where GRef is the list of file names. I get the error listed above, but I dont understand why. I have looked at this post: Error .. missing value where TRUE/FALSE needed, but I dont think it is the same thing.
You shouldn't attempt to to modify a vector while you are looping over it. The problem is your are removing items you are then trying to extract later which is causing the missing values. It's better to identify all the items you want remove first, then remove them. For example
GRef <- c("a.file.dsw", "b.file.txt", "c.file.gif", "d.file.dsw")
exts <- sapply(strsplit(GRef, "[.]"), `[`, 3)
GRef <- GRef[exts!="dsw"]

Incorrect Output If then Else statement in R

Please see the code below. the code does not show the accurate value as everything it being set to "Others" in the new var titles:
extractTitle<- function(name){
name<-as.character(name)
if (length(grep("Miss.",name))>0){
return("Miss.")
}else if (length(grep("Master.",name))>0){
return("Master.")
}else if (length(grep("Mrs.",name))>0){
return("Mrs.")
}else if (length(grep("Mr.",name))>0){
return("Mr.")
}else {
return("Other")
}
}
titles<- NULL
for (i in 1:nrow(data.combined)){
titles<- c(titles,extractTitle(data.combined[i,"name"]))
}
data.combined$title<- as.factor(titles)
Kindly advice where is the issue.
Your code works for me when trying it with simple test data ("foobar", "fooMaster.bar" "Master.bar", "fooMaster.", etc.). So the issue probably lies with how you handle the dataset to the function. For example, if you misspelled the "name" column access this will handle NULL to the function, which in turn will cause "Other" be handed back.
PS: sorry for posting this as an answer but I'm still too low for comments :)

using if/else with function in R

I have tried to write a simple code to compute the median but I got an error.
This is what I wrote
median<-function(x){odd.even<-length(x)%%2 if (odd.even = = 0)(sort(x)[length(x)/2]+sort(x)[1+length(x)/2])/2 else (sort(x)[ceiling(length(x)/2)])}
and this is the error I got
Error: unexpected 'if' in "median<-function(x){odd.even<-length(x)%%2 if"
Thanks
Try this (you forgot the brackets {)
median<-function(x){
odd.even<-length(x)%%2
if (odd.even == 0){
(sort(x)[length(x)/2]+sort(x)[1+length(x)/2])/2
} else {
(sort(x)[ceiling(length(x)/2)])
}
}
As pointed out if you want not to use bracket you can always do this, with a new line on the if statement :
median<-function(x){
odd.even<-length(x)%%2
if (odd.even == 0) (sort(x)[length(x)/2]+sort(x)[1+length(x)/2])/2 else (sort(x)[ceiling(length(x)/2)])
}
Also a return(x) at the end, might help the reading process, although it is not compulsory.

Resources