在生成行号时循环R,而不是列值

时间:2014-09-13 16:06:14

标签: r loops

我无法在#34;医院"栏中产生价值。当我循环该函数时,它会生成行号,而不是医院名称。

这是一个小例子:我创建了一个包含4列和3行(包括标题)的.csv文件。我列出了列标题:值。只有Hospital.30是数字:

hospital_name: hospital1, hospital2
Hospital.30: 12,4
death:heart, heart
State: A A

目标是从最低的医院排序医院。#30"最高的。因此,目标是拥有医院2,然后是医院1。

这是我的代码:

best <- function(states, source, id=1:2) {
    framework <- read.csv("hospital/example.csv")
    hospital_name <- "hospital_name"
    death_source <- "Hospital.30"

    mortality_list <- framework[which(framework[, "State"] == states), death_source]
    hospital_list <- framework[which(framework[, "State"] == states), hospital_name]
    numeric_mortality <- as.numeric(mortality_list)
    x <- rep(0, length(id))  # x is the column with numeric death count
    y <- rep(0, length(id))  # y is the column with the names of hospitals
    for (i in id) {
        x[i] <- sort(numeric_mortality, partial = i)[i]
        y[i] <- hospital_list[numeric_mortality == x[i]]
    }
    print(x)
    print(y)
}

所以当我尝试在控制台中运行它时:

best("A", 1:2)

我明白了:

[1]  4 12
[1] 2 1

2 1是与我得到的排序x值相关联的行号,但我为列定义了循环&#34; hospital_name&#34;。

如何让它显示hospital2 hostpital1而不是2 1

dput(head(framework))就是这样:

structure(list(hospital_name = structure(1:2, .Label = c("hospita1", 
"hospita2"), class = "factor"), Hospital.30 = c(12L, 4L), death = structure(c(1L, 
1L), .Label = "heart", class = "factor"), State = structure(c(1L, 
1L), .Label = "A", class = "factor")), .Names = c("hospital_name", 
"Hospital.30", "death", "State"), row.names = 1:2, class = "data.frame")

1 个答案:

答案 0 :(得分:1)

您的问题是数据类型。初始化y<-rep(0,length(id))时,您将y定义为数字。您的hospital_list是一个因子,它既有数字表示(2,1表示级别),也有字符表示(表示标签)。最简单的解决方法是将hospital_list转换为字符。用它替换它的定义:

hospital_list <- as.character(framework[which(framework[,"State"] == states), hospital_name])

你的问题就会消失。

作为旁注,您的which是不必要的,您可以缩短为

hospital_list <- as.character(framework[framework[,"State"] == states, hospital_name])