两个相同的数据帧,其中一个抛出错误,“仅使用第一个元素”

时间:2018-06-28 07:11:13

标签: r

I will show you the code first before ask question

    best <- function(state, outcome) {

    outcome <- read.csv(“outcome-of-care-measures.csv”, colClasses = “character”)
    hospital <- data.frame(outcome[,2], outcome[,7], outcome[,11], outcome[,17], outcome[,23], stringsAsFactors = FALSE)
    colnames(hospital) <- c(“name”, “state”, “heart attack”, “heart failure”, “pneumonia”)

        if (!state %in% hospital[ , “state”]) {
            stop(“invalid state”)
        } else if (!outcome %in% c(“heart attack”, “heart failure”, “pneumonia”)){
            stop(‘invalid outcome’)
    }

        else {
             chosen_state <- hospital[which(hospital[, “state”] == state), ] 
             chosen_outcome <- as.numeric(chosen_state[ , outcome]) 
             best_outcome <- chosen_state[,”name”][which(chosen_outcome == 
             min(chosen_outcome, na.rm = TRUE))]

    }

        best_outcome[order(best_outcome)]

    }

最佳状态错误(“ TX”,“心脏病”):无效结果         另外:警告消息:         在if(!结果%in%c(“心脏病”,“心力衰竭”,“肺炎”)){:         条件的长度> 1,并且只会使用第一个元素

    else if (!outcome %in% c(“heart attack”, “heart failure”, “pneumonia”)) <- I assume this is where it throws me an error,

    if (!state %in% hospital[ , “state”]) {
    stop(“invalid state”) <- how come this part doesn't throw me an error when the data frame has length > 1?

    Also, if you use cbind function, my best function passes somehow...
    For example,
    data <- read.csv(“outcome-of-care-measures.csv”, colClasses = “character”)
    dat <- data.frame(cbind(data[, 2], 
    data[, 7], 
    data[, 11], 
    data[, 17], 
    data[, 23]), 
    stringsAsFactors = FALSE)
    colnames(dat) <- c(“name”, “state”, “heart attack”, “heart failure”, “pneumonia”)


    apparently, both dat and hospital are identically same as data frame,
    but the result is different and i don't know why..

    outcome <- read.csv(“outcome-of-care-measures.csv”, colClasses = “character”)
    hospital <- data.frame(outcome[,2], outcome[,7], outcome[,11], outcome[,17], outcome[,23], stringsAsFactors = FALSE)
    colnames(hospital) <- c(“name”, “state”, “heart attack”, “heart failure”, “pneumonia”)

0 个答案:

没有答案