为什么我的data.frame的第二列没有打印

时间:2014-06-07 14:05:15

标签: r loops dataframe

我的第二栏需要帮助。它不是打印。我的程序正在循环给我正确的医院,但没有给我第二列国家列表。 (医院所在的州缩写)

这就是我得到的:

[1] "ALASKA REGIONAL HOSPITAL"
[1] "BAPTIST MEDICAL CENTER EAST"
[1] "BAPTIST HEALTH MEDICAL CENTER NORTH LITTLE ROCK"
[1] "CARONDELET HEART AND VASCULAR INSTITUTE"
[1] "OLYMPIA MEDICAL CENTER"

但这就是我需要的

[1] "ALASKA REGIONAL HOSPITAL"                           "AK"                                                 
[1] "BAPTIST MEDICAL CENTER EAST"                        "AL"
[1] "BAPTIST HEALTH MEDICAL CENTER NORTH LITTLE ROCK"    "AR"

这是代码

states<-unique(dat$State)
state<-(sort(states))
for (i in 1:length(state))
    statelist<-state[i]
    HospitalName<-data.frame()
    for(i in 1:length(state)) {
        if(outcome=="heart attack" && num==num) {
            sub.heart.attack <- subset(dat,State==state[i],select=c (Hospital.Name,Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack))
            order.heart.attack <- order(as.numeric(sub.heart.attack[,2],na.rm=TRUE))
            number1.heart.attack <- order.heart.attack[num]
            HospitalName <- print(sub.heart.attack$Hospital.Name[number1.heart.attack])
            statelist
        }
    }
    HospitalName<-data.frame(HospitalName,statelist)
}

0 个答案:

没有答案