Re: [問題] 請問while...done...until語法

看板R_Language作者 (Edster)時間10年前 (2015/10/31 17:06), 10年前編輯推噓0(001)
留言1則, 1人參與, 最新討論串3/3 (看更多)
: x <- c(3600, 5000, 12000, NA, 1000, 2000, 600, 7500, 1800, 9000) : ss<-0 : i<-1 : done<-FALSE : while(!done){ : if(x[i]>1){ : ss<-ss+1 : } : if(is.na(x[i])==TRUE){ : ss<-ss+0 : } : if(x[i]=="NA"){ : done<-TRUE : } : i<-i+1 : } : 這個語法還是無法正確顯示出第幾筆是missing : 目前知道判斷是否為缺失值需要使用is.na(x)的語法,但不知道如何結合 : 麻煩高手相救,謝謝 # your method x <- c(3600, 5000, 12000, NA, 1000, 2000, 600, 7500, 1800, 9000) ss<-0 i<-1 done<-FALSE while(!done){ if(x[i]>1 & !is.na(x[i]>1)){ } if(is.na(x[i])){ done<-TRUE } ss = i i<-i+1 } ss 想了一下你的bug, 在 if() 中只能吃TRUE or FALSE 例如 x[i] > 1 這是條件式, 但是若 x[i] 為 NA 則條件式失敗, 回傳 NA 舉例來說 if(TRUE) cat("TRUE","\n") if(NA) cat("Error","\n") 這時要用 TRUE & NA; 不是 TRUE | NA; 請自行比較兩者差別 其他寫法 # x = c(3600, 5000, 12000, NA, 1000, 2000, 600, 7500, 1800, 9000) # method 1 na.action(na.omit(x)) # method 2 grep("TRUE", is.na(x)) # method 3 n = length(x); s = NULL for(i in 1:n){ if(is.na(x[i])) s = c(s, i) } # method 4 i = 1; while(!is.na(x[i])){ i = i + 1 if(is.na(x[i])) cat(x[i],"\n") } 期中考到了, 我也來渡化眾生, 期望paper能accepted. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.249.20.219 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1446282396.A.84F.html ※ 編輯: Edster (111.249.20.219), 10/31/2015 17:07:55 ※ 編輯: Edster (111.249.20.219), 10/31/2015 17:41:36 ※ 編輯: Edster (111.249.20.219), 10/31/2015 17:54:39

10/31 17:51, , 1F
十分感謝 :)
10/31 17:51, 1F
文章代碼(AID): #1MD8ISXF (R_Language)
文章代碼(AID): #1MD8ISXF (R_Language)