簡體   English   中英

在 R 中創建函數時出現問題

[英]Issues creating functions in R

我在 R 中創建多步函數時遇到問題。 當我在 function 之外使用它們並將我需要的信息插入代碼行時,我的代碼行可以工作,但是當我使用 function() 並將變量插入代碼行時,它會出錯,通常會出現錯誤Error: Result must have length 370, not 0

例如,這是我現在正在使用的 function:

best <- function(state, outcome) {
    stateuse <- hospitaloutcome %>% filter(State == state)
    stateusecols <- stateuse[,c(2,7,11,17,23)]
    stateusecols <- stateusecols %>% rename('heart attack' = 
                                                'Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack')
    stateusecols <- stateusecols %>% rename('heart failure' = 
                                                'Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure')
    stateusecols <- stateusecols %>% rename('pneumonia' = 
                                                'Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia')
    stateusecols$'heart failure' <- as.numeric(stateusecols$'heart failure')
    stateusecols$'pneumonia' <- as.numeric(stateusecols$'pneumonia')
    value <- min(stateusecols$outcome , na.rm=TRUE)
    neededrow <- stateusecols %>% filter(stateusecols$outcome == value)
    hospital <- neededrow[,1]
    hospital
}

#But when I call best('TX','heart attack') it errors with the result must have length 370, not 0
#However when I just do this:

stateuse <- hospitaloutcome %>% filter(State == 'TX')
    stateusecols <- stateuse[,c(2,7,11,17,23)]
    stateusecols <- stateusecols %>% rename('heart attack' = 
                                                'Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack')
    stateusecols <- stateusecols %>% rename('heart failure' = 
                                                'Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure')
    stateusecols <- stateusecols %>% rename('pneumonia' = 
                                                'Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia')
    stateusecols$'heart failure' <- as.numeric(stateusecols$'heart failure')
    stateusecols$'pneumonia' <- as.numeric(stateusecols$'pneumonia')
    value <- min(stateusecols$'heart attack' , na.rm=TRUE)
    neededrow <- stateusecols %>% filter(stateusecols$'heart attack' == value)
    hospital <- neededrow[,1]
    hospital
#I get my answer, same lines of code, without the function() and the variables I need are in the same 
#spots as the variables defined.

我創建的所有函數都一遍又一遍地遇到這個問題。 有什么建議么?

#For reference
 str(stateusecols)
'data.frame':   370 obs. of  5 variables:
 $ Hospital.Name: chr  "PROVIDENCE MEMORIAL HOSPITAL" "MEMORIAL HERMANN BAPTIST ORANGE HOSPITAL" "PETERSON REGIONAL MEDICAL CENTER" "CHILDREN'S HOSPITAL -SCOTT & WHITE HEALTHCARE" ...
 $ State        : chr  "TX" "TX" "TX" "TX" ...
 $ heart attack : num  16.1 16.3 15.7 NA 17.4 15.7 12.9 17.4 18.1 16 ...
 $ heart failure: num  9.1 14.3 12.4 10.5 15.1 15.6 11.2 11.8 11.8 9.2 ...
 $ pneumonia    : num  12.4 12.7 14.3 11.9 13.1 10.4 12 11.4 12.1 10.2 ...

@user2554330 的解決方案

通過將所有$outcome更改為[,outcome,drop=TRUE] ,function 現在可以工作了。

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM