全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
1110 2
2018-11-11
------------------------------------------------------------------------代码
rankhospital <- function(state, outcome, num = "best") {
        ## Read outcome data
        data <- read.csv("outcome-of-care-measures.csv")
        ## Check that state and outcome are valid
        state_flags<-FALSE
        states<-levels(data[,7])[data[,7]]
        for (i in 1:length(states)) {
                if (state==states) {
                        state_flags<-TRUE
                }
        }
        if (!state_flags) {
                stop("invalid state")

        }
        if (!((outcome == "heart attack") | (outcome == "heart failure")
              | (outcome == "pneumonia"))) {
                stop("invalid outcome")

        }
        col <- if (outcome == "heart attack") {
                11
        } else if (outcome == "heart failure") {
                17
        } else {
                23
        }
        data[, 2] <- as.character(data[, 2])
        statedata <- data[grep(state, data$State), ]
        orderdata <- statedata[order(statedata[, col], statedata[, 2], na.last = NA), ]
        if(num == "best") {
                orderdata[1, 2]
        } else if(num == "worst") {
                orderdata[nrow(orderdata), 2]
        } else{
                orderdata[num, 2]
        }
        ## Return hospital name in that state with the given rank
        ## 30-day death rate
-----------------------------------------------------------------分割结束
为什么标红的类型转化语句存在与不存在会导致输出不同的结果呢,因为这个数据原本类型就是字符,为什么还需要转换类型?有大腿能帮忙答疑么?

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2018-12-4 18:50:55
帮你顶帖~~~~
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2018-12-4 18:57:40
expr       
The name of a function, or a call or an expression written as a function of x which will evaluate to an object of the same length as x.
x       
a ‘vectorizing’ numeric R function.
y       
alias for from for compatibility with plot
from, to       
the range over which the function will be plotted.
n       
integer; the number of x values at which to evaluate.
add       
logical; if TRUE add to an already existing plot; if NA start a new plot taking the defaults for the limits and log-scaling of the x-axis from the previous plot. Taken as FALSE (with a warning if a different value is supplied) if no graphics device is open.
xlim       
NULL or a numeric vector of length 2; if non-NULL it provides the defaults for c(from, to) and, unless add = TRUE, selects the x-limits of the plot – see plot.window.
type       
plot type: see plot.default.
xname       
character string giving the name to be used for the x axis.
xlab, ylab, log, ...       
labels and graphical parameters can also be specified as arguments. See ‘Details’ for the interpretation of the default for log.
For the "function" method of plot, ... can include any of the other arguments of curve, except expr.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群