if语句在for循环

时间:2016-11-19 18:07:14

标签: r loops

要运行此功能,可以在https://github.com/Bheal/Board-Q-A找到csv文件outcome-of-care-measures.csv。 这篇文章只关注我函数中循环内部使用的代码部分:

if(num=="best"){num=1}
if(num=="worst") {num=nrow(df); print(num)}

我把这个功能放在了一起。我有一个想法(新手,我是)要做什么,但几乎每一步都需要调整一些东西来获得所需的功能。 但我的另一个障碍是我似乎无法在循环中添加一个元素,以便if-statment为变量num分配新值(如果num ="最差"是函数输入)。 (见下文# ***

rankall <- function( outcome, num = "best") {
        ## Read outcome data
        tmp <- read.csv("outcome-of-care-measures.csv",na.strings="Not Available")

                b1 <- outcome %in% c("heart attack","heart failure","pneumonia")

        # if(){stop()}
        if(b1==FALSE){ stop("Invaled output name")}

        if(outcome=="heart attack") {i=11}
        if(outcome=="heart failure") {i=17}
        if(outcome=="pneumonia") {i=23}

        t1<-as.vector(unique(tmp[['State']]))

        #initialize a df for storage        
        dfall<- data.frame("H.name"=as.character(), "S.name"=as.character(), stringsAsFactors = FALSE)


        for(x in 1:length(t1)) {                                # begin a loop, each state abb.

                df <- subset(tmp, State==t1[x], select= c(2,i)) # subset data.frame, for state abb., & select column with Hospital name & i(outcome col).
                df <- subset(df, !is.na(df[2]))                 # remove rows with na's in the outcome col from this data.frame.

# *** *** ***

print(dim(df))  # *** for each loop the dim(df) function is reset, but I can't get the num below in the to reset using the if statement.
        # *** However if 

                if(num=="best"){num=1}
                if(num=="worst") {num=nrow(df); print(num)}     # ***   this only prints one time, and is equal to the no. of rows in df from the first loop.
# *** *** ***

                df <- df[order(df[2],df[1]), ]                  # order this data.frame. by outcome(primary) and Hosptial(secondary).

                df[[1]] <- as.character(df[[1]])                # Class of First column of df changed: was class factor, changed to class char.


                entry <- c(df[num,1],t1[x])

                dfall <- rbind(dfall,entry, stringsAsFactors = FALSE)   # ? I have to use stringsAsFactors=FALSE, else dfall won't populate properly.

        }

  names(dfall) <- c("Hospital", "State")          # ? If I don not assign these names, d.f. dfall has wrong names(tied to 1st entry), not H.name,S.name. 
  return(dfall)
} 

如果num在函数调用中等于一个整数,那么我对num的依赖是有效的,但在num =&#34;最差&#34;我需要为每次迭代提取一个特定的编号条目。 (如果dim(df) =&#34;最好&#34;这不会影响结果,因为它对应于每次迭代中的第一行)。 为什么if语句不受for循环的每次迭代的影响? df正在每个循环中重置,print(dim(df))也会发生变化,如下面的if(num=="best"){num=1} if(num=="worst") {num=nrow(df); print(num)} 输出所示

> rankall("pneumonia", "worst")
[1] 91  2
[1] 91
[1] 14  2
[1] 65  2
[1] 73  2
        .
        .
        .
        .
                                            Hospital State
1                    JACKSONVILLE MEDICAL CENTER    AL
2                                           <NA>    AK
3                                           <NA>    AZ
4                                           <NA>    AR
5                        MARINA DEL REY HOSPITAL    CA
6                                           <NA>    CO
.
.
.

如输出中所示,第二行给出了打印91(如果num =&#34则在剩余的循环中使用num = 91;在函数调用中最差&#34;

form1 <- ~ psource + cond + psource:cond

提前致谢。

1 个答案:

答案 0 :(得分:3)

试试这个(只是为了表明我的评论意思)。您希望保留函数调用中给出的num参数,并将其用于每次迭代。我在下面的代码中添加了一个重置​​。

rankall2 <- function( outcome, num = "best") {
    ## Read outcome data
    tmp <- read.csv("outcome-of-care-measures.csv",na.strings="Not Available")

    b1 <- outcome %in% c("heart attack","heart failure","pneumonia")

    # if(){stop()}
    if(b1==FALSE){ stop("Invaled output name")}

    if(outcome=="heart attack") {i=11}
    if(outcome=="heart failure") {i=17}
    if(outcome=="pneumonia") {i=23}

    t1<-as.vector(unique(tmp[['State']]))

    #initialize a df for storage        
    dfall<- data.frame("H.name"=as.character(), "S.name"=as.character(), stringsAsFactors = FALSE)
    ## Keep the original num
    original.num <- num 

    for(x in 1:length(t1)) {                                # begin a loop, each state abb.
        ## Reset num
        num <- original.num

        df <- subset(tmp, State==t1[x], select= c(2,i)) # subset data.frame, for state abb., & select column with Hospital name & i(outcome col).
        df <- subset(df, !is.na(df[2]))                 # remove rows with na's in the outcome col from this data.frame.

# *** *** ***

        print(dim(df))  # *** for each loop the dim(df) function is reset, but I can't get the num below in the to reset using the if statement.
        # *** However if 

        if(num=="best"){num=1}
        if(num=="worst") {num=nrow(df); print(num)}     # ***   this only prints one time, and is equal to the no. of rows in df from the first loop.
# *** *** ***

        df <- df[order(df[2],df[1]), ]                  # order this data.frame. by outcome(primary) and Hosptial(secondary).

        df[[1]] <- as.character(df[[1]])                # Class of First column of df changed: was class factor, changed to class char.

        entry <- c(df[num,1],t1[x])

        dfall <- rbind(dfall,entry, stringsAsFactors = FALSE)   # ? I have to use stringsAsFactors=FALSE, else dfall won't populate properly.

    }

    names(dfall) <- c("Hospital", "State")          # ? If I don not assign these names, d.f. dfall has wrong names(tied to 1st entry), not H.name,S.name. 
    return(dfall)
}