R 参数未正确传递到函数中
RStudio版本:1.0.143版 Windows版本:Windows10 Pro 我正在编写一个函数,用于返回每个状态下请求的医院R 参数未正确传递到函数中,r,R,RStudio版本:1.0.143版 Windows版本:Windows10 Pro 我正在编写一个函数,用于返回每个状态下请求的医院 函数的结构如下:根据我们在评论中的交流,rankall。这应该给你同样的答案: rankall <- function(outcome, num = "best") { ## Read outcome data datful <- read.csv("outcome-of-care-measures.csv", col
函数的结构如下:根据我们在评论中的交流,rankall。这应该给你同样的答案:
rankall <- function(outcome, num = "best") {
## Read outcome data
datful <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
## Check that state and outcome are valid
if(outcome == "heart attack"){
oc <- 11
} else if(outcome == "heart failure"){
oc <- 17
} else if(outcome == "pneumonia"){
oc <- 23
} else {
"invalid outcome"
}
## For each state, find the hospital of the given rank
StaUni <- unique(datful[,7])
StaUni <- sort(StaUni)
sta_req <- c()
hos_req <- c()
out_req <- c()
V <- 1
for(i in StaUni){
##For each state, assemble a data.frame
good <- datful[, 7] == i
# print(tail(good))
Sta_i <- datful[, 7][good]
Hos_i <- datful[, 2][good]
Out_i <- as.numeric(datful[, oc][good])
out_na <- is.na(Out_i)
Sta_i <- Sta_i[!out_na]
Hos_i <- Hos_i[!out_na]
Out_i <- Out_i[!out_na]
Obs_i <- data.frame(Sta_i, Hos_i, Out_i)
##Reoder each data.frame
Obs_i <- Obs_i[order(Obs_i[, 3], Obs_i[, 2]),]
print(tail(Obs_i))
## Change the value of num depend on it's value.
if(num == "best"){
V <- 1
} else if(num == "worst"){
V <- length(Sta_i)
} else {
V <- V
}
hos_req[i] <- as.character(Obs_i[V, 2])
sta_req[i] <- as.character(Obs_i[V, 1])
out_req[i] <- as.numeric(Obs_i[V, 3])
}
## Return a data frame with the hospital names and the
## (abbreviated) state name
DFReq <- cbind(hos_req, sta_req)
DFReq
}
rankall@ChiPak正如我所想,你的解决方案不起作用。False是0,True是1,因此sum(good)将是相同的。无论如何,谢谢。@Ryan如果你解决了自己的问题,请将其作为答案发布,而不是在问题正文中编辑。这将有助于未来的读者找到解决方案。@Llopis我没有找到答案,第二部分不是函数,我只是将参数的测试值放在函数中,这样它就可以计算测试值的答案,效果很好。但是如果我使用带有测试值的函数,结果是错误的。@ChiPak函数没有退出。好的,你不会得到相同的答案,因为在函数rankall
中,你对两个变量使用num
num
最初用作字符参数num=“best”
<代码>如果(num==“最差”)
将num
更改为sum(good)
。下次通过迭代,num==sum(good)
分配num=num
。只需更改其中一个num
,您应该会得到相同的答案。我已经尝试了您的代码,只是将“v=v”更改为“v=num”,这会在每个单元格中给出一个NA的结果。顺便问一下,你是如何在这里的注释中添加代码格式的?我不知道为什么,但是在我运行了你的代码之后,我的原始函数现在也可以工作了。我会在家里的另一台电脑上再次检查。正如你所说,这是“for”循环中的问题,我可以通过将“V”改为“num”来重复我的问题。非常感谢你。
>tail(rankall("pneumonia", "worst"), 3)
hos_req sta_req
WI "MINISTRY DOOR COUNTY MEDICAL CENTER" "WI"
WV "MONTGOMERY GENERAL HOSPITAL, INC" "WV"
WY "EVANSTON REGIONAL HOSPITAL" "WY"
There were 46 warnings (use warnings() to see them)
## Read outcome data
datful <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
## Check that state and outcome are valid
## For each state, find the hospital of the given rank
StaUni <- unique(datful[,7])
StaUni <- sort(StaUni)
sta_req <- c()
hos_req <- c()
out_req <- c()
for(i in StaUni){
##For each state, assemble a data.frame
good <- datful[, 7] == i
Sta_i <- datful[, 7][good]
Hos_i <- datful[, 2][good]
Out_i <- as.numeric(datful[, 23][good])
out_na <- is.na(Out_i)
Sta_i <- Sta_i[!out_na]
Hos_i <- Hos_i[!out_na]
Out_i <- Out_i[!out_na]
Obs_i <- data.frame(Sta_i, Hos_i, Out_i)
##Reoder each data.frame
Obs_i <- Obs_i[order(Obs_i[, 3], Obs_i[, 2]),]
## Change the value of num depend on it's value.
hos_req[i] <- as.character(Obs_i[length(Sta_i), 2])
sta_req[i] <- as.character(Obs_i[length(Sta_i), 1])
out_req[i] <- as.numeric(Obs_i[length(Sta_i), 3])
}
## Return a data frame with the hospital names and the
## (abbreviated) state name
DFReq <- cbind(hos_req, sta_req)
DFReq
WI "MAYO CLINIC HEALTH SYSTEM - NORTHLAND, INC" "WI"
WV "PLATEAU MEDICAL CENTER" "WV"
WY "NORTH BIG HORN HOSPITAL DISTRICT" "WY"
rankall <- function(outcome, num = "best") {
## Read outcome data
datful <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
## Check that state and outcome are valid
if(outcome == "heart attack"){
oc <- 11
} else if(outcome == "heart failure"){
oc <- 17
} else if(outcome == "pneumonia"){
oc <- 23
} else {
"invalid outcome"
}
## For each state, find the hospital of the given rank
StaUni <- unique(datful[,7])
StaUni <- sort(StaUni)
sta_req <- c()
hos_req <- c()
out_req <- c()
V <- 1
for(i in StaUni){
##For each state, assemble a data.frame
good <- datful[, 7] == i
# print(tail(good))
Sta_i <- datful[, 7][good]
Hos_i <- datful[, 2][good]
Out_i <- as.numeric(datful[, oc][good])
out_na <- is.na(Out_i)
Sta_i <- Sta_i[!out_na]
Hos_i <- Hos_i[!out_na]
Out_i <- Out_i[!out_na]
Obs_i <- data.frame(Sta_i, Hos_i, Out_i)
##Reoder each data.frame
Obs_i <- Obs_i[order(Obs_i[, 3], Obs_i[, 2]),]
print(tail(Obs_i))
## Change the value of num depend on it's value.
if(num == "best"){
V <- 1
} else if(num == "worst"){
V <- length(Sta_i)
} else {
V <- V
}
hos_req[i] <- as.character(Obs_i[V, 2])
sta_req[i] <- as.character(Obs_i[V, 1])
out_req[i] <- as.numeric(Obs_i[V, 3])
}
## Return a data frame with the hospital names and the
## (abbreviated) state name
DFReq <- cbind(hos_req, sta_req)
DFReq
}