2016-11-19 10 views
0

この機能を実行するには、https://github.com/Bheal/Board-Q-Aにcsvファイルのoutcome-of-care-measures.csvがあります。 この投稿は以下の私の機能にループ内で使用されるコードのこの部分に関する:forループの各反復でリセットされないif文

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

私は一緒にこの機能を入れています。私は何をすべきかというアイデア(初心者、私です)を持っていましたが、ほとんどの段階で目的の機能を得るために何かを微調整する必要がありました。 しかし、私の残る1つのハードルは、if- statmentが変数num(num = "worst"が関数入力である場合)に新しい値を割り当てるように、ループに要素を追加するように見えないということです。それは関数呼び出しではなく、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 = "best"の場合は結果に影響しませんが、それは各繰り返しで最初の行に対応するためです)。 forループの各繰り返しでifステートメントが実行されないのはなぜですか?

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

の下print(dim(df))の出力によって証明されるように、2行目が印刷を残りのループ91(及びその後NUM = 91が使用さを与える出力に見られるように DFは、各ループにリセットされ、あまりにもdim(df)変更されます関数呼び出しでnum = "worst"の場合)

> 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]、decreing = T)]] – Nate

+0

申し訳ありませんが私の問題を再現するために、私は' num'を置き換えようとしています前記data.frameの最後の行の関数呼び出しにおいて最悪に等しいと見なされ、forループ内のif文を使用してそのようにすることを望んでいた。 – Bhail

+1

これは 'num'を上書きするためです。最初の反復では、 'num =" worst "'があり、それを数値で置き換えます。 2回目の反復では、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

関連する問題