审核R编程课程(coursera)
我已经尝试过检查知道其他解决方案并以交互方式运行我的代码来理解错误,现在我正绕着我自己的迭代盘旋。
收到追溯错误:
顺序错误(NULL,整数(0),na.last = TRUE,减去= FALSE): 参数1不是矢量 6阶(NULL,整数(0),na.last = TRUE,减去= FALSE) 5 do.call(“order”,c(z,na.last = na.last,减少=减少)) 4个订单(complete_data $ outcome,complete_data $ name) 3
[.data.frame
(complete_data,order(complete_data $ outcome,complete_data $ name), )在rankhospital.R#60 2 complete_data [order(complete_data $ outcome,complete_data $ name), ]在rankhospital.R#60 1 rankhospital(“TX”,“心力衰竭”,4)
我的代码:
rankhospital <- function(state, outcome, num = "best") {
## Read outcome data
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
state_col <- data[ , 7]
name_col <- data[ , 2]
attack_col <- suppressWarnings(as.numeric(data[ , 11]))
failure_col <- suppressWarnings(as.numeric(data[ , 17]))
pneumonia_col <- suppressWarnings(as.numeric(data[ , 23]))
best_data <- cbind(state_col, name_col, attack_col, failure_col, pneumonia_col)
colnames(best_data) <- c("state", "name", "heart attack", "heart failure", "pneumonia")
best_data <- as.data.frame(best_data)
## Check that state and outcome are valid
list_outcomes <- c("heart attack", "heart failure", "pneumonia")
if (!(outcome %in% list_outcomes))
stop("invalid outcome")
list_unique_states <- unique(state_col)
if (!(state %in% list_unique_states))
stop("invalid state")
## Return hospital name in that state with the given rank
## 30-day death rate
state_data <- best_data[best_data$state == state, ]
#complete_data <- state_data[complete.cases(state_data[,"outcome"]), ]
complete_data <- state_data[!is.na(state_data[state_data$"outcome", ]), ]
complete_data <- as.data.frame(complete_data)
#have data order the outcome by name,
ordered_data <- complete_data[order(complete_data$"outcome", complete_data$name), ]
final_data <- ordered_data
min_value <- which.min(final_data[, outcome])
max_value <- which.max(final_data[, outcome])
if (num == "best") {
as.character(final_data[min_value, 2])
} else
if (num == "worst") {
as.character(final_data[max_value, 2])
} else
{as.character(final_data[num, 2])
}
}
}
当我以交互方式运行代码时,使用示例行60逐行工作就可以了。
答案 0 :(得分:0)
要选择函数参数outcome
中的列,请使用state_data[, outcome]
,而不是state_data$"outcome"
:
download.file("https://github.com/Yang-Zhou/computing-for-data-analysis/blob/master/proj2/outcome-of-care-measures.csv?raw=true", tf<-tempfile(fileext = ".csv"))
rankhospital <- function(state, outcome, num = "best") {
## Read outcome data
data <- read.csv(tf, colClasses = "character")
state_col <- data[ , 7]
name_col <- data[ , 2]
attack_col <- suppressWarnings(as.numeric(data[ , 11]))
failure_col <- suppressWarnings(as.numeric(data[ , 17]))
pneumonia_col <- suppressWarnings(as.numeric(data[ , 23]))
best_data <- cbind(state_col, name_col, attack_col, failure_col, pneumonia_col)
colnames(best_data) <- c("state", "name", "heart attack", "heart failure", "pneumonia")
best_data <- as.data.frame(best_data)
## Check that state and outcome are valid
list_outcomes <- c("heart attack", "heart failure", "pneumonia")
if(!(outcome %in% list_outcomes))
stop("invalid outcome")
list_unique_states <- unique(state_col)
if(!(state %in% list_unique_states))
stop("invalid state")
## Return hospital name in that state with the given rank
## 30-day death rate
state_data <- best_data[best_data$state == state, ]
#complete_data <- state_data[complete.cases(state_data[,"outcome"]), ]
# complete_data <- state_data[!is.na(state_data[state_data$"outcome", ]), ]
complete_data <- state_data[!is.na(state_data[, outcome]), ]
complete_data <- as.data.frame(complete_data)
#have data order the outcome by name,
ordered_data <- complete_data[order(complete_data[, outcome], complete_data$name), ]
final_data <- ordered_data
min_value <- which.min(final_data[, outcome])
max_value <- which.max(final_data[, outcome])
if(num == "best"){
as.character(final_data[min_value, 2])
}else
if(num == "worst"){
as.character(final_data[max_value, 2])
}else
{as.character(final_data[num, 2])
}
}
rankhospital("AL", "heart attack")