if statement - R - " missing value where TRUE/FALSE needed " -
this question has answer here:
i'm trying execute following code in r
comments = c("no","yes",na) (l in 1:length(comments)) { if (comments[l] != na) print(comments[l]); }
but i'm getting error
error in if (comments[l] != na) print(comments[l]) : missing value true/false needed
what's going on here?
check command : na!=na
: you'll result na
, hence error message.
you have use function is.na
if
statement work (in general, better use function check na
values) :
comments = c("no","yes",na) (l in 1:length(comments)) { if (!is.na(comments[l])) print(comments[l]) } [1] "no" [1] "yes"
Comments
Post a Comment