使用R中的包进行范围问题

时间:2014-05-30 13:43:07

标签: r plyr scoping

我这里有这个代码。

library(plyr)

rankhospital <- function(state, illness, rank) {
    outcome <- read.csv("data3/outcome.csv")

    hA <- cbind(as.numeric(as.character(outcome[,11][outcome$State == state])), 
                as.character(outcome$Hospital.Name[outcome$State == state]))
    hF <- cbind(as.numeric(as.character(outcome[,17][outcome$State == state])), 
                as.character(outcome$Hospital.Name[outcome$State == state]))
    pN <- cbind(as.numeric(as.character(outcome[,23][outcome$State == state])), 
                as.character(outcome$Hospital.Name[outcome$State == state]))

    condition <- ifelse(illness == "heart attack", list(hA), ifelse(illness == "heart   failure", list(hF), 
                                                    ifelse(illness == "pneumonia", list(pN), "not a valid input")))

    outcomeData <- as.data.frame(condition)

    rates <- outcomeData[,1]
    hospitals <- outcomeData[,2]

    outcomeData$rank <- ddply(outcomeData,.(hospitals),transform,Order =rank(rates,ties.method = "first")) 

    print(outcomeData$rank)
}

我可以使用排名功能,但我想先按医院的字母顺序排名。我不能用rank函数做到这一点。每当我使用一个包来按其他变量排名时,我都会收到此错误。

Error in head(outcomeData) : object 'outcomeData' not found

我已经搜索了这个问题,似乎我有一个与R的词法范围规则有关的问题,但是,我很困惑,因为我已经使用了函数以外的函数,我不明白为什么我我在这里遇到这个问题。

0 个答案:

没有答案