Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. ## R Programming Assignment 3 - Part III
  2.  
  3. ## TASKS:
  4. ## (1) Plot the 30-day mortality rates for heart attack
  5. ## (2) Finding the best hospital in a state
  6. ## (3) Ranking hospitals by outcome in a state
  7. ## -> (4) Ranking hospitals in all states
  8.  
  9.  
  10. ## (4) Ranking hospitals in all states
  11.  
  12. rankall <- function (outcome_name, num = "best") {
  13.  
  14. setwd("/Users/lucky1eva/Downloads/rprog-data-ProgAssignment3-data/")
  15. outcome <- read.csv("outcome-of-care-measures.csv")
  16.  
  17. # Check validity
  18. if (!outcome_name %in% c("heart attack", "heart failure", "pneumonia")) {
  19.  
  20. stop("invalid outcome")
  21.  
  22. }
  23.  
  24. # Subset relevant data from all states
  25. outcome <- subset(outcome, select = c("Hospital.Name","State",
  26. "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack",
  27. "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure",
  28. "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"))
  29.  
  30. # Assign colnames to match the arguments in terms of outcome_name
  31. colnames(outcome) <- c("Name","State", "heart attack", "heart failure", "pneumonia")
  32.  
  33. # Correct classes of each variable
  34. outcome[, 3:5] <- apply(outcome[, 3:5], 2, function(x) as.numeric(x))
  35. outcome[, 1:2] <- apply(outcome[, 1:2], 2, function(x) as.character(x))
  36.  
  37. # Restructure the data frame and keep the class unchanged
  38. outcome_by_state <- split(outcome, outcome$State) # split outcome table by state
  39. outcome_table <- lapply(outcome_by_state, as.data.frame) # set each element class to data.frame
  40.  
  41. # define a function to use lapply() to set new order for each table at once
  42. f <- function (x, y) {x[order(x[, y], x[, 1]), ]}
  43.  
  44. sorted_table <- lapply(outcome_table, f, y = outcome_name)
  45.  
  46. all_listed <- NULL # just to define the return variable used in the following
  47.  
  48. for (i in 1:54) { # check in each state if the rank requested by 'num' is numeric / char and if within range
  49.  
  50. if (class(num) == "numeric" && num <= sum(!is.na(sorted_table[[i]][, outcome_name])) && num >= 1) {
  51.  
  52. all_listed <- rbind(all_listed, sorted_table[[i]][num, 1:2])
  53.  
  54. } else if (class(num) == "numeric" && num > sum(!is.na(sorted_table[[i]][, outcome_name])) ) {
  55.  
  56. all_listed <- rbind(all_listed, data.frame("Name" = NA, "State" = names(sorted_table[i])))
  57.  
  58. } else if ( num == "best") {
  59.  
  60. all_listed <- rbind(all_listed, sorted_table[[i]][1, 1:2])
  61.  
  62. } else if (num == "worst") {
  63.  
  64. all_listed <- rbind(all_listed, sorted_table[[i]][sum(!is.na(sorted_table[[i]][, outcome_name])), 1:2])
  65.  
  66. }
  67. }
  68.  
  69.  
  70. all_listed
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement