Hi everyone, Beginner here in "R-programming". I am taking some online course for credit (Coursera) and got an assignment of creating 3 different functions in "R". I created the functions, which works but does not get me the result I expect it to give. But if I pass the individual steps separately, it does work and gets me the expected result. Need help troubleshooting what is going on here in this function/code. If anyone is familiar with this, please provide some hint/help. Help will be much appreciated. Thanks. Cheers.
SuperDad · Jul 19, 2015 10:40 PM · 9,504 views
@super provide us what you have done so far. then will we will be able to look on it. Last edited: 20-Jul-15 02:50 AM
mero_naam_ho · Jul 19, 2015 11:12 PM
Thanks for response @mero_naam_ho Do you want me to post the info and my code, here in this thread? or inbox you the detail, what question is asking and what I have done so far?
SuperDad · Jul 19, 2015 11:16 PM
If you post your work here then in addition to me others will be able to help you out too. Please state your requirement clearly before posting your code. Let us know what you expect and what is not working. Last edited: 20-Jul-15 02:53 AM
mero_naam_ho · Jul 20, 2015 2:53 AM
Assignment provides a csv file of "hospital data" consisting of information from all the hospitals across the US, with different variables like Hospital name, State, mortality due to heart attack, heart failure, pneumonia. File link: https://class.coursera.org/rpr... So the assignment asks us to write 3 different functions. So the first question goes like this: 1. Write a function called best that take two arguments: the 2-character abbreviated name of a state and an outcome name. The function reads the outcome-of-care-measures.csv file and returns a character vector with the name of the hospital that has the best (i.e. lowest) 30-day mortality for the specified outcome in that state. The hospital name is the name provided in the Hospital.Name variable. The outcomes can be one of “heart attack”, “heart failure”, or “pneumonia”. Hospitals that do not have data on a particular outcome should be excluded from the set of hospitals when deciding the rankings. Hint: The function should use the following template. best best("TX", "heart attack") [1] "CYPRESS FAIRBANKS MEDICAL CENTER" > best("TX", "heart failure") [1] "FORT DUNCAN MEDICAL CENTER" My solution to this: (SOLUTION 1) best rankhospital("MD", "heart failure", 5) would return a character vector containing the name of the hospital with the 5th lowest 30-day death rate for heart failure. The num argument can take values “best”, “worst”, or an integer indicating the ranking (smaller numbers are better). If the number given by num is larger than the number of hospitals in that state, then the function should return NA. Hospitals that do not have data on a particular outcome should be excluded from the set of hospitals when deciding the rankings. Hint: The function should use the following template. rankhospital rankhospital("TX", "heart failure", 4) [1] "DETAR HOSPITAL NAVARRO" [1] "HARFORD MEMORIAL HOSPITAL" > rankhospital("MN", "heart attack", 5000) [1] NA My Solution: SOLUTION2 #Writing a function named "rankhospital" rankhospital <-function(state, outcome, num) { data <- read.csv("ProgAssignment3-data/outcome-of-care-measures.csv", colClasses="character") subset_data <- subset(data, data$State=="state") #acquires subset of data for particular #state where State="state" if (outcome == "heart attack") { subset_data[,11] <-as.numeric(subset_data[,11]) dat <- subset_data[order(subset_data[,11],na.last=NA),] #orders/sorts all the column of the #subsetted data in ascending order based on 11th column, removing all the NA values (na.list=NA) hospital <-dat$Hospital.Name[num] } if (outcome == "heart failure") { subset_data[,17] <-as.numeric(subset_data[,17]) dat <- subset_data[order(subset_data[,17],na.last=NA),] hospital<-dat$Hospital.Name[num] } if (outcome == "pneumonia") { subset_data[,23] <-as.numeric(subset_data[,23]) dat <- subset_data[order(subset_data[,23],na.last=NA),] hospital <-dat$Hospital.Name[num] } return(hospital) } Here in this part, I do not get any warning or error message, however, I get [1] NA , as an output. For Question #3, I am still in the process of writing the function. I know this pieces of informations got longer. But to provide all the details of assignment without loosing the information I thought this was the only way. Anyone's help will be highly highly appreciated. Thanks in advance.
SuperDad · Jul 20, 2015 9:24 AM
yeah I am asking too much, but I just cant' download that CSV file,I believe I need to enrolled in the course, so please put the few rows from that file (about 10 rows) or upload that file to google drive or somewhere and provide the link. Last edited: 20-Jul-15 12:31 PM
mero_naam_ho · Jul 20, 2015 12:26 PM
Here I have copied the file to my google drive and shared the file. https://drive.google.com/file/... Thanks.
SuperDad · Jul 20, 2015 1:40 PM
There is a problem viewing your document
microbiology · Jul 20, 2015 1:56 PM
Here is the Google DOCS version of that file: https://docs.google.com/spread...
maxpayne · Jul 20, 2015 2:09 PM
Thank Maxpayne. Since Maxpayne has now posted the new link to DOCS version of this file. I guess, it should work now as it worked in my case. May be you need to Download the file from google drive link in above case to view it.
SuperDad · Jul 20, 2015 2:13 PM
make sure that your csv file is under your working directory. you get the working directory by following command getwd() When i installed R on my computer the curret working directory was my documents. If you are using windows and haven't changed configuration yours should be also mydocuments. Assignment 1 Taken from here https://gist.github.com/timmys... best <- function(state, outcome) { data <- read.csv(file='outcome-of-care-measures.csv', colClasses = 'character') if(!any(state == data$State)) { stop('invalid state') } if(outcome == 'heart attack') { i <- 11 } else if(outcome == 'heart failure') { i <- 17 } else if(outcome == 'pneumonia') { i <- 23 } else { stop('invalid outcome') } # print(i) # todo: handle the ties data.state <- data[data$State == state, ] data.state[, i] <- as.numeric(x=data.state[, i]) data.state <- data.state[complete.cases(data.state), ] # print(data.state[, c(1, 2, i)]) # print(data.state[, i]) # min(data.state[, i]) -> mm # print(mm) # print(min(data.state[, i], na.rm=TRUE)) # print(data.state[, i] == min(data.state[, i])) return.names <- data.state[(data.state[, i] == min(data.state[, i])), ]$Hospital.Name sort(return.names)[1] } best("TX", "heart attack") For other assignment check this link: http://rstudio-pubs-static.s3.... Last edited: 20-Jul-15 03:46 PM Last edited: 20-Jul-15 03:46 PM
mero_naam_ho · Jul 20, 2015 3:46 PM
नक्कलको लागि पनि अक्कल चाहिन्छ भन्थे, हो रैछ | धुनिपत्तल लगाएर गूगल अनि stackoverflow मा खोज्दा नि नभेटिएको कुरो ऐले येसरी छ्याप्छ्याप्ती भेतिएछ | Thats pretty damn awesome. This solved my problem. Which is good. But I am still not sure what was wrong with my code. I am very happy with this result. But if you could explain what happened with my function, I will be even more happy. Anyways thank you very much @mero_naam_ho. You ROCK. Thanks again.
SuperDad · Jul 20, 2015 4:44 PM
www.stackoverflow.com is where you post this
Hydrogen Sulphide · Jul 20, 2015 5:08 PM
Got U @H2S. Thanks for the suggestion. I agree with you. Next time I get a problem, I will definitely do that.
SuperDad · Jul 20, 2015 5:13 PM
1. I haven't looked much at the code when i found that your code was not reading the file. It was returning a function instead of data with bunch of errors. 2. Not sure what are you trying to do at this line subset_data <- subset(data, data$State=="state") I guess you are trying to read the variable (state) but as you have enclosed it with double quotation sign, instead of reading the variable it will always be "state" and thus will return no matching data. 3. I am not sure what "output" == "state" does? or don't know what are you trying to do here. cause that statement always returns false because here again you are trying to compare a string with another string. Even if you meant to use "output" as a variable I don't see any output variable anywhere in the code. 4. at the end the statement return(dat$Hospital.Name) is invalid because the variable dat has local scope (i.e . it is valid within the if statement only) and since it doesn't have a global scope that statement will result an error. After that I stopped looking at your code and started getting help from google. Glad that it helped.Last edited: 20-Jul-15 05:31 PM Last edited: 20-Jul-15 05:32 PM
mero_naam_ho · Jul 20, 2015 5:24 PM
Thanks @Mero_naam_ho for your time and input. Definitely it helped, and I understood what mistake I was doing. Also I have now understood, how I should have created that function, still keeping my approach the same. I am learning slowly and steadily. Thanks again.
SuperDad · Jul 20, 2015 7:18 PM
This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.
Start a New Discussion