2016-11-19 57 views
0

要运行此函数,可以在https://github.com/Bheal/Board-Q-A处找到csv文件care-measures-csv。 这篇文章只关注下面我功能的循环中使用的这部分代码:如果语句不会为for循环的每次迭代重置

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

我已经把这个功能在一起。我有一个想法(新手,我)做什么,但几乎每一步都需要调整一些东西以获得所需的功能。 但我剩下的一个障碍是,我似乎无法添加一个元素到我的循环,以便if-statment将新值赋给变量num(如果num =“worst”是函数输入)。 (见下文# ***

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 =“最好”不会影响结果,因为它对应于每次迭代中的第一行)。 为什么if语句不会受for循环的每次迭代影响? DF是被在每个循环复位和dim(df)变化太大如下

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

证明的print(dim(df))输出作为输出端看到的第二行给出打印91(然后NUM = 91用来在剩余的环如果在函数调用中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 
. 
. 
. 

在此先感谢。

+0

我会建议调整排序'if(num ==“worst”)''而不是试图抓住一个数字位置。 'df < - df [order(df [2],df [1],decrease = T),]' – Nate

+0

对不起,我不重复,但为了重申我的问题,等同于在所述data.frame的最后一行的函数调用中最差,并且希望在for循环中使用if语句来这样做。 – Bhail

+1

这是因为你覆盖了'num'。在第一次迭代中,例如'num =“worst”',并且用一个数字替换它。在第二次迭代中,'num'是一个值,因此没有您的条件匹配 – ekstroem

回答

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

我会在将来留意,我相信还有其他的方法可以改善,但是解决了这个问题。再次感谢。 – Bhail