Re: [問題] 請問while...done...until語法
: 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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章
26
152