Curious
Curious

Reputation: 11

Function returns not only the value, but also factor levels?

Here's the problem I'm confused about. I have a hospital quality data frame. The variables I use here look like that:

str(ff)
'data.frame':   4706 obs. of  7 variables:
$ Provider.Number                                           : Factor w/4706 levels "010001","010005",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Hospital.Name                                             : Factor w/ 4510 levels "ABBEVILLE AREA MEDICAL CENTER",..: 3587 2150 1048 2506 838 2149 3828 910 3490 465 ...
$ City                                                      : Factor w/ 2926 levels "ABBEVILLE","ABERDEEN",..: 698 269 870 1910 1511 1064 241 900 19 241 ...
$ State                                                     : Factor w/ 54 levels "AK","AL","AR",..: 2 2 2 2 2 2 2 2 2 2 ...
$ Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack : num  14.3 18.5 18.1 NA NA NA 17.7 18 15.9 NA ...
$ Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure: num  11.4 15.2 11.3 13.6 13.8 12.5 10.9 16.6 13.6 NA ...
$ Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia    : num  10.9 13.9 13.4 14.9 15.8 8.7 16.2 15.8 10.7 NA ...

I've written a function in R which is supposed to return the best hospital in the state (with the lowest Mortality Rate). Here is this function:

best <- function(state, outcome) {
data <- read.csv("outcome-of-care-measures.csv", na.strings = "Not     Available")
if (state %in% data$State == FALSE) 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")
x <- subset(data, data$State == state)
best_h <- x[which.min(x[,i]), ]
best_h[1,2]
}

It works and returns the right result and the output looks as this:

best("MD", "
[1] GREATER BALTIMORE MEDICAL CENTER
4510 Levels: ABBEVILLE AREA MEDICAL CENTER ... ZUNI COMPREHENSIVE COMMUNITY HEALTH CENTER

The question is - How can I get rid of the 4510 Levels... in my output?

Upvotes: 1

Views: 406

Answers (1)

noname
noname

Reputation: 482

You can convert to a character vector with as.character(best_h[1,2]).

Upvotes: 1

Related Questions