diff --git a/Assignment 2.docx b/Assignment 2.docx new file mode 100644 index 00000000000..846832217e2 Binary files /dev/null and b/Assignment 2.docx differ diff --git a/BISassignment2.docx b/BISassignment2.docx new file mode 100644 index 00000000000..d5b5b94b92e Binary files /dev/null and b/BISassignment2.docx differ diff --git a/FinallyGoodLuck b/FinallyGoodLuck new file mode 100644 index 00000000000..c0030e79c91 --- /dev/null +++ b/FinallyGoodLuck @@ -0,0 +1,150 @@ + + +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 + +source("sortHospitalsByOutcome.R") +head(sortHospitalsByOutcome(state, outcome), 1) +} +outcomeCol.R +outcomeCol <- function(outcome) { +if (outcome == "heart attack") { +outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack" +} else if (outcome == "heart failure") { +outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure" +} else if (outcome == "pneumonia") { +outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia" +} +else { +stop("invalid outcome") +} +} +rankall.R +rankall <- function(outcome, num="best") { +## Read outcome data +## Check that state and outcome are valid +## For each state, find the hospital of the given rank +## Return a data frame with the hospital names and the +## (abbreviated) state name + +source("outcomeCol.R") + +outcome <- outcomeCol(outcome) + +data <- read.csv("data/outcome-of-care-measures.csv", colClasses="character") +data[,outcome] <- suppressWarnings(as.numeric(data[,outcome])) +data <- data[order(data$"State", data[outcome], data$"Hospital.Name", na.last=NA),] +data <- data[!is.na(outcome)] + +l <- split(data[,c("Hospital.Name")], data$State) + +rankHospitals <- function(x, num) { +if (num=="best") { +head(x, 1) +} else if (num=="worst") { +tail(x, 1) +} else { +x[num] +} +} + +result <- lapply(l, rankHospitals, num) +data.frame(hospital = unlist(result), state = names(result), row.names = names(result)) +} +rankhospital.R +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 + +source("best.R") +source("sortHospitalsByOutcome.R") + +if (num=="best") { +best(state, outcome) +} else if (num=="worst") { +tail(sortHospitalsByOutcome(state, outcome), 1) +} else { +sortHospitalsByOutcome(state, outcome)[num] +} +} +sortHospitalsByOutcome.R +sortHospitalsByOutcome <- function(state, outcome) { +source("outcomeCol.R") +outcome <- outcomeCol(outcome) + +data <- read.csv("data/outcome-of-care-measures.csv", stringsAsFactors=FALSE) + +if (!state %in% data$State) { +stop("invalid state") +} + +data <- data[data$State==state,] + +data[,outcome] <- suppressWarnings(as.numeric(data[,outcome])) +data <- data[order(data[outcome], data$"Hospital.Name"),] +as.character(data$"Hospital.Name"[!is.na(data[outcome])]) +} +@Cosmin11 +Cosmin11 commented on 21 Mar 2015 + +best <- function(state, outcome ) { + +#Read outcome data---------------------------------------- +my_data= read.csv("outcome-of-care-measures.csv",colClasses = "character") + +#ERROR MSG for Outcomes----------------------------------- + +if (outcome == "heart attack"){ + + my_outcome = 11 + + } else if (outcome == "heart failure"){ + + my_outcome = 17 + + }else if (outcome == "pneumonia"){ + + my_outcome = 23 + + } else { + + stop ("invalid outcome") + } + +#ERROR MSG for State-------------------------------------- +my_state = as.character(state) +my_states =as.vector(unique(my_data$State)) + + if (any(my_state == my_states,na.rm = TRUE)){ + + }else{ + + stop("invalid state") + } + +#Create Data Frame with Hosp., State, H. Attack, H. Failure & Pneumonia----- +my_sample<-my_data[which(my_data[,7]==my_state),c(2,7,my_outcome)] + +#Replace 'Not Available with NA--------------------------------------------- +my_sample_without_NA<-my_sample[which(my_sample[,3]!="Not Available"),] + +#Find minimum value of the Outcome-------------------------------------------- +my_minimum<-min(as.numeric(my_sample_without_NA[,3])) + +#Display all hospitals with the minimum dead rate----------------------------- +my_result<-my_sample_without_NA[which(as.numeric(my_sample_without_NA[,3])==my_minimum),] + +#Display result in Alphabetical ordered------------------------------------------ +my_ordered_result<-my_result[order(my_result[,1]),] + +#Display first value from the result vector------------------------------------- +my_final_result<-as.vector(head(my_ordered_result[,1],1)) + + my_final_result + +} diff --git a/GitHub.exe b/GitHub.exe new file mode 100644 index 00000000000..fc7024624f9 Binary files /dev/null and b/GitHub.exe differ diff --git a/GoodLuck b/GoodLuck new file mode 100644 index 00000000000..2aeea1ebcc9 --- /dev/null +++ b/GoodLuck @@ -0,0 +1,29 @@ +This function is a list of objects in the form of a function that computes the mean of a vector and caches it if not yet done: + +makeVector <- function(x = numeric()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmean <- function(mean) m <<- mean + getmean <- function() m + list(set = set, get = get, + setmean = setmean, + getmean = getmean) +} + +This function computes, if not done, the inverse of a the matrix and retrieves it. + +cachemean <- function(x, ...) { + m <- x$getmean() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- mean(data, ...) + x$setmean(m) + m +} diff --git a/Hospital Statistics b/Hospital Statistics new file mode 100644 index 00000000000..e1a260b5329 --- /dev/null +++ b/Hospital Statistics @@ -0,0 +1,89 @@ +best <- function(state, outcome) { +## Read outcome +## validate that state and outcome are OK +## Return hospital name in state where 30-day death is lowest +## rate +source("sortHospitalsByOutcome.R") +head(sortHospitalsByOutcome(state, outcome), 1) +} +outcomeCol.R +outcomeCol <- function(outcome) { +if (outcome == "heart attack") { +outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack" +} else if (outcome == "heart failure") { +outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure" +} else if (outcome == "pneumonia") { +outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia" +} +else { +stop("invalid outcome") +} +} + +rankall.R +rankall<- function(outcome, num="best") { +## Read outcome +## Validate that state and outcome are OK +## For each state find hospital of the given rank +## Return a dataframe with hospital names and +## state name + +source("outcomeCol.R") + +outcome <- outcomeCol(outcome) + +data <- read.csv("data/outcome-of-care-measures.csv", colClasses="character") +data[,outcome] <- suppressWarnings(as.numeric(data[,outcome])) +data <- data[order(data$"State", data[outcome], data$"Hospital.Name", na.last=NA),] +data <- data[!is.na(outcome)] + +l <- split(data[,c("Hospital.Name")], data$State) + +rankHospitals <- function(x, num) { +if (num=="best") { +head(x, 1) +} else if (num=="worst") { +tail(x, 1) +} else { +x[num] +} +} + +result <- lapply(l, rankHospitals, num) +data.frame(hospital = unlist, state = names, row.names = names) +} +rankhospital.R +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 + +source("best.R") +source("sortHospitalsByOutcome.R") + +if (num=="best") { +best(state, outcome) +} else if (num=="worst") { +tail(sortHospitalsByOutcome(state, outcome), 1) +} else { +sortHospitalsByOutcome(state, outcome)[num] +} +} +sort Hospitals By Outcome.R +sort Hospitals By Outcome <- function(state, outcome) { +source("outcomeCol.R") +outcome <- outcomeCol(outcome) + +data <- read.csv("data/outcome-of-care-measures.csv", stringsAsFactors=FALSE) + +if (!state %in% data$State) { +stop("invalid state") +} + +data <- data[data$State==state,] + +data[,outcome] <- suppressWarnings(as.numeric(data[,outcome])) +data <- data[order(data[outcome], data$"Hospital.Name"),] +as.character(data$"Hospital.Name"[!is.na(data[outcome])]) +} diff --git a/Hospital Statistics.docx b/Hospital Statistics.docx new file mode 100644 index 00000000000..5cf30cacc85 Binary files /dev/null and b/Hospital Statistics.docx differ diff --git a/MakeVector.docx b/MakeVector.docx new file mode 100644 index 00000000000..0494357c201 Binary files /dev/null and b/MakeVector.docx differ diff --git a/matrixinverse b/matrixinverse new file mode 100644 index 00000000000..2ebcf687403 --- /dev/null +++ b/matrixinverse @@ -0,0 +1,35 @@ + + +## makeCacheMatrix function creates a "matrix" object that caches its inverse. +#1. set value of the matrix +#2. get value of the matrix +#3. set value of the inverse +#4. get value of the inversemakeCacheMatrix <- function(x = matrix()) { + + j <- NULL + set <- function(y) { + x <<- y + j <<- NULL + } + get <- function() x + setinverse <- function(solve) j <<- solve + getinverse <- function() j + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) + +} +## `cacheSolve` function computes the inverse of the "matrix" returned by `makeCacheMatrix` above. +#If the inverse has already been calculated (and the matrix has not changed), then`cacheSolve` should retrieve the inverse from the cache. + +## when giving: Error in x$getinverse : $ operator is invalid for atomic vectors +cachesolve <- function(x, ...) { + i<- x$getinverse() + if(!is.null(j)) { + message("getting cached data") + return(j) + } + data <- x$get() + j<- solve(data, ...) + x$setinverse(j) + j + +}