RStudio Ver:版本1.0.143
Windows Ver:Windows10 Pro
我正在写一个函数来返回每个州要求的医院。
函数的结构如下:rankall< - function(outcome,num =“best”)
outcome
是排序结果的标准,num
告诉函数选择排名。
该函数可以使用num = "best"
和other numeric numbers
返回正确的结果,但无法使用"worst"
提供正确的结果。函数的代码是这样的:
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()
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[, 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]),]
## Change the value of num depend on it's value.
if(num == "best"){
num = 1
} else if(num == "worst"){
num = sum(good)
} else {
num = num
}
hos_req[i] <- as.character(Obs_i[num, 2])
sta_req[i] <- as.character(Obs_i[num, 1])
out_req[i] <- as.numeric(Obs_i[num, 3])
}
## Return a data frame with the hospital names and the
## (abbreviated) state name
DFReq <- cbind(hos_req, sta_req)
DFReq
}
结果如下:
>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)
要查看问题所在,我尝试将"pneumonia"
提供给outcome
,将"worst"
提交给num
。其他一切都像功能一样(除了它不是一个功能,我只想看看问题出在哪里)。代码是这样的:
## 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
结果的最后3行是这样的(这是正确答案):
WI "MAYO CLINIC HEALTH SYSTEM - NORTHLAND, INC" "WI"
WV "PLATEAU MEDICAL CENTER" "WV"
WY "NORTH BIG HORN HOSPITAL DISTRICT" "WY"
任何人都可以告诉我在num = "worse"
时使用该功能会改变什么?
对于任何想要运行代码的人来说,这是数据:
https://dl.dropboxusercontent.com/u/1174148/outcome-of-care-measures.csv
答案 0 :(得分:0)
根据我们在评论中的交流。这应该给你相同的答案:
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
}