Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .RData
Binary file not shown.
12 changes: 12 additions & 0 deletions .Rhistory
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
List.files()
list.files()
source("cachematrix.R")
ls()
git add .
outcome <-read.csv("outcome-of-care-measures.csv" colClasses="character")
outcome <-read.csv("outcome-of-care-measures.csv",colClasses="character")
View(outcome)
head(outcome)
clear
View(outcome)
q()
Binary file added Hospital_Revised_Flatfiles.pdf
Binary file not shown.
42 changes: 42 additions & 0 deletions best.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
best <- function(state, outcome) {
## Read outcome data
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")

## Check that state is valid
if (!state %in% data$State) {
stop("invalid state")
}

## Check that outcome is valid
valid_outcomes <- c("heart attack", "heart failure", "pneumonia")
if (!outcome %in% valid_outcomes) {
stop("invalid outcome")
}

## Map outcomes to their respective column indices
outcome_col <- c(
"heart attack" = 11,
"heart failure" = 17,
"pneumonia" = 23
)

## Filter data for the specified state
state_data <- data[data$State == state, ]

## Extract and convert rates to numeric
rates <- as.numeric(state_data[, outcome_col[outcome]])

## Filter out NA values
valid <- !is.na(rates)
state_data <- state_data[valid, ]
rates <- rates[valid]

## Find the hospital with the lowest rate
lowest <- min(rates)

best_hospitals <- state_data$Hospital.Name[rates == lowest]

## Sort alphabetically in case of a tie and return the first one
best_hospitals <- sort(best_hospitals)
return(best_hospitals[1])
}
37 changes: 31 additions & 6 deletions cachematrix.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
## Put comments here that give an overall description of what your
## functions do
## These two functions work together to cache the inverse of a matrix,
## so that if the inverse has already been calculated for the current
## matrix, it can be retrieved from the cache instead of recomputed.

## Write a short comment describing this function
## makeCacheMatrix creates a special "matrix" object that can cache
## its inverse. It returns a list of functions to set and get the
## value of the matrix, and to set and get the value of the inverse.

makeCacheMatrix <- function(x = matrix()) {

inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(inverse) inv <<- inverse
getinverse <- function() inv
list(set = set,
get = get,
setinverse = setinverse,
getinverse = getinverse)
}


## Write a short comment describing this function
## cacheSolve computes the inverse of the special "matrix" returned
## by makeCacheMatrix. If the inverse has already been calculated
## (and the matrix has not changed), it retrieves the inverse from
## the cache instead of recomputing it.

cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinverse(inv)
inv
}
4,827 changes: 4,827 additions & 0 deletions hospital-data.csv

Large diffs are not rendered by default.

4,707 changes: 4,707 additions & 0 deletions outcome-of-care-measures.csv

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions rankall.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
rankall <- function(outcome, num = "best") {
## Read outcome data
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")

valid_outcomes <- c("heart attack", "heart failure", "pneumonia")
if (!outcome %in% valid_outcomes) {
stop("invalid outcome")
}

outcome_col <- c(
"heart attack" = 11,
"heart failure" = 17,
"pneumonia" = 23
)

col_index <- outcome_col[outcome]
states <- sort(unique(data$State))

hospital_vector <- c()
state_vector <- c()

for (s in states) {
state_data <- data[data$State == s, ]
rates <- as.numeric(state_data[, col_index])

valid <- !is.na(rates)
state_data <- state_data[valid, ]
rates <- rates[valid]

ordered_indices <- order(rates, state_data$Hospital.Name)
state_data <- state_data[ordered_indices, ]

total_hospitals <- nrow(state_data)

selected_hospital <- NA
if (total_hospitals > 0) {
if (num == "best") {
selected_hospital <- state_data$Hospital.Name[1]
} else if (num == "worst") {
selected_hospital <- state_data$Hospital.Name[total_hospitals]
} else if (is.numeric(num)) {
if (num <= total_hospitals) {
selected_hospital <- state_data$Hospital.Name[num]
}
}
}

hospital_vector <- c(hospital_vector, selected_hospital)
state_vector <- c(state_vector, s)
}

return(data.frame(hospital = hospital_vector, state = state_vector, row.names = state_vector))
}
50 changes: 50 additions & 0 deletions rankhospital.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
rankhospital <- function(state, outcome, num = "best") {
## Read outcome data
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")

## Check that state and outcome are valid
if (!state %in% data$State) {
stop("invalid state")
}

valid_outcomes <- c("heart attack", "heart failure", "pneumonia")
if (!outcome %in% valid_outcomes) {
stop("invalid outcome")
}

outcome_col <- c(
"heart attack" = 11,
"heart failure" = 17,
"pneumonia" = 23
)

## Filter by state and extract valid rate rows
state_data <- data[data$State == state, ]
rates <- as.numeric(state_data[, outcome_col[outcome]])

valid <- !is.na(rates)
state_data <- state_data[valid, ]
rates <- rates[valid]

## Order hospitals by rate and then by name alphabetically
ordered_indices <- order(rates, state_data$Hospital.Name)
state_data <- state_data[ordered_indices, ]

## Handle num argument ("best", "worst", or numeric rank)
total_hospitals <- nrow(state_data)

if (num == "best") {
target_row <- 1
} else if (num == "worst") {
target_row <- total_hospitals
} else if (is.numeric(num)) {
if (num > total_hospitals) {
return(NA)
}
target_row <- num
} else {
stop("invalid num")
}

return(state_data$Hospital.Name[target_row])
}
Binary file added rprog_data_ProgAssignment3-data.zip
Binary file not shown.