Skip to content

Instantly share code, notes, and snippets.

@byanuaria
Created June 6, 2018 23:07
Show Gist options
  • Save byanuaria/4edb752c71b345dfc8a4ea4cab6c06aa to your computer and use it in GitHub Desktop.
Save byanuaria/4edb752c71b345dfc8a4ea4cab6c06aa to your computer and use it in GitHub Desktop.
John Hopkins Introduction to Programming Assignment 3
library(tidyverse)
# read in the data
outcome <- read.table("rprog_data_ProgAssignment3-data/outcome-of-care-measures.csv",
header = TRUE, sep = ",", colClasses = "character")
nrow(outcome)
ncol(outcome)
names(outcome)
# coerce column 11 from character to numeric, NA's introduced
outcome[,11] <- as.numeric(outcome[,11])
# problem 1
# create a histogram and plot 30-day mortality rates for
# heart attacks
hist(outcome[,11])
# problem 2
# find the best hospital in a state
setwd("rprog_data_ProgAssignment3-data/")
outcomes <- read.csv2("outcome-of-care-measures.csv", sep = ",", header = TRUE, colClasses = "character")
# convert column 7 and 11 (the ones I want) to a numeric and character
# instead of factor
outcomes[,7] <- as.character(outcomes[,7])
outcomes[,11] <- as.numeric(as.character(outcomes[,11]))
outcomes[,2] <- as.characteR(outcomes[,2])
# subset based on state
alaska <- subset(outcomes, State == "AK")
# index the min value
which.min(alaska[,11]) # input this in the next problem (see below)
# find the hospital in that index
myval <- as.character(alaska[which.min(alaska[,11]),2])
myval
data <- read.csv("outcome-of-care-measures.csv",
header = TRUE, sep = ",",
colClasses = "character")
###########################################################################################
### this works but only works for heart attack column (col 11) NOT the rest ###############
########### figure out how to add the second outcome argument #############################
foo <- function(thestate, outcome) {
# read outcome data
data <- read.csv("outcome-of-care-measures.csv",
header = TRUE, sep = ",",
colClasses = "character")
# convert to proper classes
data[,7] <- as.character(data[,7])
data[,11] <- as.numeric(data[,11])
for(i in thestate) {
# choose the state
mystate <- subset(data, State == thestate)
# find the hospital
myhospital <- as.character(mystate[which.min(mystate[,11]), 2])
}
myhospital
}
#######################################################################
########################### failing so hard ###########################
################### attempt to add the second argument ################
fooey <- function(thestate, outcome) {
# read outcome data
data <- read.csv("outcome-of-care-measures.csv",
header = TRUE, sep = ",", colClasses = "character")
# create and convert to proper classes
heart_attacks <- as.numeric(data[, 11])
heart_failure <- as.numeric(data[, 17])
pneumonia <- as.numeric(data[, 23])
foovec <- numeric()
outcome <- function(outcome) {
if(outcome == "heart attack") {
foovec <- heart_attacks
} else if(outcome == "heart failure") {
foovec <- heart_failure
} else if(outcome == "pneumonia") {
foovec <- pneumonia
} else {
print("invalid outcome")
}
}
for(i in thestate) {
# choose the state
mystate <- subset(data, State == thestate)
# find the lowest rate
index <- which.min(foovec)
# find the hospital
myhospital <- as.character(mystate[index])
}
foovec
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment