作为Coursera数据专业化的一部分,我不得不分析美国医院的数据集,并编写以下功能:
该功能应该执行以下操作:
我希望将医院的名称作为长度为1的字符的向量,但我得到了#34;字符(0)"作为输出
所需输出的示例:
best("TX", "pneumonia")
[1] "UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER"
实际输出示例:
best("TX", "pneumonia")
character(0)
请帮我看看我的代码出了什么问题。非常感谢
这是我的代码:
## This function returns the best hospital in a state, given the disease types
## based on its mortality rate in 30-day period
best <- function(state, outcome) {
## calling data
outcomedata <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
## putting needed data into data frame
outcomedf <- as.data.frame(cbind(outcomedata[, 2], outcomedata[, 7],
outcomedata[, 11], outcomedata[, 17],
outcomedata[, 23]),
stringsAsFactors = FALSE)
colnames(outcomedf) <- c("hospital", "state", "heart attack", "heart failure", "pneumonia")
## Checking valid state
if(!state %in% outcomedf[, "state"]){
stop('invalid state')
## Checking valid outcome
}
else if(!outcome %in% c("heart attack", "heart failure", "pneumonia")){
stop('invalid outcome')
## Calling out best hospital
}
else {
## Extracting data for given state
obs_in_called_states <- which(outcomedf[, "state"] == state)
obs_in_states_extract <- outcomedf[obs_in_called_states, ]
oi <- as.numeric(obs_in_states_extract[, eval(outcome)])
## getting the min value
minvalue <- min("oi", na.rm = TRUE)
result <- obs_in_states_extract[, "hospital"][which(oi == minvalue)]
output <- result[order(result)]
}
return(output)
}
答案 0 :(得分:0)
如果您的数据框中包含姓名和分数,例如
names <- c("name1","name2","name3")
scores <- c(1,3,4)
dataset1 <- data.frame(names,scores)
使用以下方法仅在列分数中找到最低分数:
min(dataset1$scores)
要在具有最小值的另一列(例如名称列)中查找相应的向量,请使用:
paste(dataset1$names[dataset1$scores==min(dataset$scores)])
如果要按字母顺序对退货进行排序,请使用sort()函数:
paste(sort(dataset1$names[dataset1$scores==min(dataset1$scores)]))
注意,这不是针对计算效率进行优化的,而是为了可读性而编写的。 SO之间有许多资源可用于优化您的工作。