2016-11-19 23 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