I am trying work on a coursera assignment in R. My code works correctly for assignment 1 where I write a function in R to rank Hospitals; in assignment 2 I have to add few IF or IF ELSE IF statements to the function already written.
Function gives a final dataframe.
- IF input argument
num==best, function returns the first row of final data frame - IF input argument
num==worst, function returns the last row of final data frame - If input argument
num > max row countof final data frame, function returns NA - If input argument
num< max row count, function returns that row from data frame
Now, the if statement are working correctly for only scenario 3 and 4. For 1 and 2 scenarios, it is returning NA- which is the return value of scenario 3.
There is something wrong with the way I am writing IF statements ( can be sequence or the condition or something else) because of which I am getting NA return value for scenario 1,2
Code below, TIA
outcomeDF<-outcome[,c(2,7,n)]
names(outcomeDF)<-c("Hospital","State","Outcomess")
finalDF<-filter(outcomeDF,outcomeDF$State==sta)
DFSlist<-arrange(finalDF,finalDF$State,finalDF$Outcomess,finalDF$Hospital)
if (num > nrow(DFSlist)) print ("NA")
else if (num < nrow(DFSlist)) c<-(DFSlist[num,])
else if (num =="best")c<-(DFSlist[1,])
else (num =="worst")c<-(DFSlist[(nrow(DFSlist)),])
return(c)
Aucun commentaire:
Enregistrer un commentaire