R-programming. Need help -Code Troubleshooting. - Sajha Mobile
SAJHA MOBILE
R-programming. Need help -Code Troubleshooting.
Posts 16 · Viewed 7814 · Go to Last Post
SuperDad
· Snapshot 0
Like · Likedby · 0
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.
mero_naam_ho
· Snapshot 22
Like · Liked by · 0
@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
SuperDad
· Snapshot 24
Like · Liked by · 0
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?

mero_naam_ho
· Snapshot 69
Like · Liked by · 0
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
SuperDad
· Snapshot 143
Like · Liked by · 0
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/rprog-030/assignment/view?assignment_id=7
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 <- function(state, outcome) {
## Read outcome data
## Check that state and outcome are valid
## Return hospital name in that state with lowest 30-day death
## rate
}

eg:
> 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 <-function (state, outcome) {
data <- read.csv("ProgAssignment3-data/outcome-of-care-measures.csv", colClasses="character")
subset_data <- subset(data, data$State=="state")
if ("outcome" == "heart attack") {
subset_data[,11] <-as.numeric(subset_data[,11])
dat <- subset(subset_data, subset_data[,11]== min(subset_data[,11], na.rm=TRUE))
}
if ("outcome" == "heart failure") {
subset_data[,17] <-as.numeric(subset_data[,17])
dat <- subset(subset_data, subset_data[,17]== min(subset_data[,17], na.rm=TRUE))
}
if ("outcome" == "pneumonia") {
subset_data[,23] <-as.numeric(subset_data[,23])
dat <- subset(subset_data, subset_data[,23]== min(subset_data[,23], na.rm=TRUE))
}
return(dat$Hospital.Name)
}


For this part, I get the error message:
"Error in best("TX", "heart failure") : object 'dat' not found"
However running individual command step by step, without the use of "if else" function gives the output as above example.




Question 2.
Write a function called rankhospital that takes three arguments: the 2-character abbreviated name of a state (state), an outcome (outcome), and the ranking of a hospital in that state for that outcome (num). The function reads the outcome-of-care-measures.csv file and returns a character vector with the name of the hospital that has the ranking specified by the num argument. For example, the call

> 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 <- function(state, outcome, num = "best") {
## Read outcome data
## Check that state and outcome are valid
## Return hospital name in that state with the given rank
## 30-day death rate
}

eg.
> 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.
mero_naam_ho
· Snapshot 225
Like · Liked by · 0
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
SuperDad
· Snapshot 261
Like · Liked by · 0
Here I have copied the file to my google drive and shared the file.

https://drive.google.com/file/d/0B0JhZXReDXRdZGZYWGNEaVUwTEU/view?usp=sharing

Thanks.
microbiology
· Snapshot 277
Like · Liked by · 0
There is a problem viewing your document
maxpayne
· Snapshot 287
Like · Liked by · 0
Here is the Google DOCS version of that file:

https://docs.google.com/spreadsheets/d/1QPmino7mLpjWKJTFD4GDskWoe2gNNKNwUQIESJNPiqQ/edit?usp=sharing
SuperDad
· Snapshot 293
Like · Liked by · 0
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.
mero_naam_ho
· Snapshot 340
Like · Liked by · 0
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/timmyshen/6892429

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.amazonaws.com/2104_db4f612f1f2c477b91c8a456fb014c9e.html


Last edited: 20-Jul-15 03:46 PM
Last edited: 20-Jul-15 03:46 PM
SuperDad
· Snapshot 366
Like · Liked by · 0
नक्कलको लागि पनि अक्कल चाहिन्छ भन्थे, हो रैछ |
धुनिपत्तल लगाएर गूगल अनि 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.
Hydrogen Sulphide
· Snapshot 387
Like · Liked by · 0
www.stackoverflow.com is where you post this
SuperDad
· Snapshot 389
Like · Liked by · 0
Got U @H2S. Thanks for the suggestion. I agree with you. Next time I get a problem, I will definitely do that.
mero_naam_ho
· Snapshot 391
Like · Liked by · 0
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
SuperDad
· Snapshot 428
Like · Liked by · 0
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.
Please log in to reply to this post

You can also log in using your Facebook
View in Desktop
What people are reading
You might like these other discussions...
· Posts 42 · Viewed 4833 · Likes 11
· Posts 1 · Viewed 67
· Posts 3 · Viewed 254
· Posts 1 · Viewed 90
· Posts 14 · Viewed 701
· Posts 1 · Viewed 85
· Posts 5 · Viewed 464
· Posts 13 · Viewed 8671
· Posts 1 · Viewed 113
· Posts 3 · Viewed 707



Travel Partners
Travel House Nepal