Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which.min does not produce any output

Tags:

r

I have a dataset (Hospitals in the USA) where I need to get from a subset (Hospitals in a certain state) from the dataframe the row where a certain column (e.g. survival of Heart Attacks) has its minimum.

    test <- function(state, outcome) {
    ## Read outcome data
    ## Check that state and outcome are valid


      datasubset ## subsetting datta
      targetrow <- datasubset[which.min(datasubset$outcome),] ##get the row where "outcome" is minimum
      ##get hospital name where outcome is minimum
      ##get the minimum value
      ##just there to check if function works until this point
    }

If I run the function, the datasubset is printed but for the other two print commands I get character(0) and NULL

However, if I insert the code manually row for row and change state and outcome manually I get the right results. I do not really understand why it is not working when I use the function, but working when I write the commands directly into R. I suppose there is a problem with which.min ? Thanks in advance for help

(I know this is part of the R-Course from the John Hopkins University, however course if over and I still want to get a working function! It is making me crazy)

the data looks like this:

      Hospital.Name                  State heart attack heart failure pneumonia
4262 CENTRAL VERMONT MEDICAL CENTER    VT         15.4          13.7      11.4
    enter code here

I could also upload it if someone wants to reproduce it. EDIT: Code edited to avoid people just copying this code for their course.

like image 739
Gnusper Avatar asked Nov 29 '25 14:11

Gnusper


1 Answers

The error here is that you are using the $ operator for indexing. datasubset$outcome refer to the column outcome (which you do not have in your data frame).

# Refer to column with the name that is stored in the variable outcome
datasubset[which.min(datasubset[,outcome]),]

# Refer to column that have the name outcome
datasubset[which.min(datasubset$outcome),]

Run this code to further understand the difference between $ and [ ]

df <- data.frame(x=1:5,y=6:10)

x <- "y"

df$x  #Gives x column
df[,x] #Gives y column
like image 137
nist Avatar answered Dec 01 '25 03:12

nist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!