r - 在 R 中创建函数的问题
问题描述
我在 R 中创建多步函数时遇到问题。当我在函数外部使用它们并将我需要的信息插入代码行时,我的代码行工作,但是当我使用 function() 并将变量插入代码行时代码,它错误,通常有错误Error: Result must have length 370, not 0
例如,这是我现在正在使用的功能:
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]
现在可以工作。
谢谢!
推荐阅读
- javascript - 使用连接的 android 设备测试 nativescript-vue 应用程序
- javascript - 获取发送 EMPTY 对象到 Node 服务器的 http POST 请求
- node.js - “dotenv:采购'.env'文件时出错”?
- javascript - 如何使用 mCustomScrollbar 代替默认的浏览器滚动条?
- linux - 有没有办法在不使用期望或生成的情况下在 shell 脚本中执行 ssh 时自动回答用户提示?
- html - svg线条对底部边框的奇怪效果
- python - 将无 DB 字段模型添加到 django 序列化 JSON
- winforms - 通过 C# 类库中的 Web 套接字连接打开 winforms FolderBrowserDialog
- java - 如何在独立 JBoss 中配置自定义日志记录
- php - 添加 js 代码后出现 php 语法错误