I'm trying to execute the following code in R
comments = c("no","yes",NA)
for (l in 1:length(comments)) {
if (comments[l] != NA) print(comments[l]);
}
But I'm getting an error
Error in if (comments[l] != NA) print(comments[l]) : missing value where TRUE/FALSE needed
What's going on here?
check the command : NA!=NA
: you'll get the result NA
, hence the error message.
You have to use the function is.na
for your if
statement to work (in general, it is always better to use this function to check for NA
values) :
comments = c("no","yes",NA)
for (l in 1:length(comments)) {
if (!is.na(comments[l])) print(comments[l])
}
[1] "no"
[1] "yes"
Can you change the if condition to this:
if (!is.na(comments[l])) print(comments[l]);
You can only check for NA values with is.na().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With