[Show all top banners]

SuperDad
Replies to this thread:

More by SuperDad
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 R-programming. Need help -Code Troubleshooting.
[VIEWED 7750 TIMES]
SAVE! for ease of future access.
Posted on 07-19-15 10:40 PM     Reply [Subscribe]
Login in to Rate this Post:     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.
 
Posted on 07-19-15 11:12 PM     [Snapshot: 22]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 07-19-15 11:16 PM     [Snapshot: 24]     Reply [Subscribe]
Login in to Rate this Post:     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?


 
Posted on 07-20-15 2:53 AM     [Snapshot: 69]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 07-20-15 9:24 AM     [Snapshot: 143]     Reply [Subscribe]
Login in to Rate this Post:     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.
 
Posted on 07-20-15 12:26 PM     [Snapshot: 225]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 07-20-15 1:40 PM     [Snapshot: 261]     Reply [Subscribe]
Login in to Rate this Post:     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.
 
Posted on 07-20-15 1:56 PM     [Snapshot: 277]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

There is a problem viewing your document
 
Posted on 07-20-15 2:09 PM     [Snapshot: 287]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Here is the Google DOCS version of that file:

https://docs.google.com/spreadsheets/d/1QPmino7mLpjWKJTFD4GDskWoe2gNNKNwUQIESJNPiqQ/edit?usp=sharing
 
Posted on 07-20-15 2:13 PM     [Snapshot: 293]     Reply [Subscribe]
Login in to Rate this Post:     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.
 
Posted on 07-20-15 3:46 PM     [Snapshot: 340]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 07-20-15 4:44 PM     [Snapshot: 366]     Reply [Subscribe]
Login in to Rate this Post:     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.
 
Posted on 07-20-15 5:08 PM     [Snapshot: 387]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

www.stackoverflow.com is where you post this
 
Posted on 07-20-15 5:13 PM     [Snapshot: 389]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Got U @H2S. Thanks for the suggestion. I agree with you. Next time I get a problem, I will definitely do that.
 
Posted on 07-20-15 5:24 PM     [Snapshot: 391]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 07-20-15 7:18 PM     [Snapshot: 428]     Reply [Subscribe]
Login in to Rate this Post:     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 be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 30 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
nrn citizenship
ढ्याउ गर्दा दसैँको खसी गनाउच
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
कल्लाई मुर्ख भन्या ?
Travelling to Nepal - TPS AP- PASSPORT
Morning dharahara
मन भित्र को पत्रै पत्र!
Guess how many vaccines a one year old baby is given
emergency donation needed
Elderly parents travelling to US (any suggestions besides Special Assistance)?
जाडो, बा र म……
Susta Susta Degree Maile REMIXED version
1974 AD Pinjadako Suga Remixed
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters