2016-04-04 30 views
0

審計ř編程過程(coursera)在R,爲了錯誤(NULL,整數(0),na.last = TRUE,減少= FALSE):參數1不是矢量

我試圖審查瞭解其他解決方案並以交互方式運行我的代碼以瞭解錯誤,現在我圍繞自己的迭代進行盤旋。

getting traceback error:

Error in order(NULL, integer(0), na.last = TRUE, decreasing = FALSE) : argument 1 is not a vector 6 order(NULL, integer(0), na.last = TRUE, decreasing = FALSE) 5 do.call("order", c(z, na.last = na.last, decreasing = decreasing)) 4 order(complete_data$outcome, complete_data$name) 3 [.data.frame (complete_data, order(complete_data$outcome, complete_data$name), ) at rankhospital.R#60 2 complete_data[order(complete_data$outcome, complete_data$name), ] at rankhospital.R#60 1 rankhospital("TX", "heart failure", 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

哪條線是線60? – m02ph3u5

+0

它是ordered_data < - complete_data [order(complete_data $「results」,complete_data $ name), – bitrot

回答

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") 
+0

謝謝,它解決了錯誤,(我明白了) – bitrot

相關問題