哪一个最小值不产生任何输出

时间:2022-05-04 21:24:11

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

如果我运行这个函数,数据集将被打印,但是对于另外两个打印命令,我将获得字符(0)和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

但是,如果我手动插入行代码并手动更改状态和结果,我将得到正确的结果。我不明白为什么在使用这个函数时它不能工作,而是在直接将命令写入r时工作。最小值?谢谢你的帮助。

(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)

(我知道这是约翰霍普金斯大学R-Course的一部分,但是如果结束了,我还是想要一个工作函数!)这让我发疯)

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.

如果有人想复制,我也可以上传。编辑:代码被编辑以避免人们在他们的课程中复制这些代码。

1 个解决方案

#1


3  

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).

这里的错误是您正在使用$操作符进行索引。datasubset$outcome引用列结果(在您的数据框架中没有)。

# 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

#1


3  

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).

这里的错误是您正在使用$操作符进行索引。datasubset$outcome引用列结果(在您的数据框架中没有)。

# 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