Incorrect Output If then Else statement in R - 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 :)

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

Using audio_play_sound() in a if statement GameMaker

I am trying to use the command:
audio_play_sound()
I am trying to insert it into this piece of code, so that when the player jumps, a sound plays.
if (key_jump) && (jumps > 0)
{
jumps -=1;
vsp = -jumpspeed;
}
Code that causes problem:
if (key_jump) && (jumps > 0)
{
jumps -=1;
vsp = -jumpspeed;
audio_play_sound(snd_jump)
}
Simply inserting the line into the if statement does not work, and gives the error WRONG NUMBER OF ARGUMENTS IN FUNCTION. This is rather confusing, perhaps I am using the wrong command? Thanks in advance
The problem is stated in the error, you're providing the wrong number of arguments to the audo_play_sound function.
from the docs
audio_play_sound(index, priority, loop);
As the person above states your answer is audio_play_sound(snd_jump, 1, false).

function with dot(.) and comma(,) put together

map_wc <- function(.,lines)
{
lines_lst = unlist(strsplit(lines,"\r\n",fixed=TRUE))
l_cnt<-1;
keys_l<-c()
data_l<-c()
for (line in lines_lst)
{
words = unlist(strsplit(line,";",fixed=TRUE))
if (length(words) != 5){
next
}
}
Anyone using Rhadoop might be familiar with the code above. Can someone please explain to me the reason why ".," i.e dot(.) and (,) in the first line function are placed side-by-side. Please when answering note that am new to R.

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.

undefined columns selected error message in 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)

Resources