Newbie syntax difficulties with R if/then/else structure [duplicate] - r

I received this error message:
Error in if (condition) { : missing value where TRUE/FALSE needed
or
Error in while (condition) { : missing value where TRUE/FALSE needed
What does it mean, and how do I prevent it?

The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.
if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed
This can happen accidentally as the results of calculations:
if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed
To test whether an object is missing use is.na(x) rather than x == NA.
See also the related errors:
Error in if/while (condition) { : argument is of length zero
Error in if/while (condition) : argument is not interpretable as logical
if (NULL) {}
## Error in if (NULL) { : argument is of length zero
if ("not logical") {}
## Error: argument is not interpretable as logical
if (c(TRUE, FALSE)) {}
## Warning message:
## the condition has length > 1 and only the first element will be used

I ran into this when checking on a null or empty string
if (x == NULL || x == '') {
changed it to
if (is.null(x) || x == '') {

this works with "NA" not for NA
comments = c("no","yes","NA")
for (l in 1:length(comments)) {
#if (!is.na(comments[l])) print(comments[l])
if (comments[l] != "NA") print(comments[l])
}

I was getting this same error in my forloops with complex if statements. I fixed this issue by just wrapping my condition with isTRUE.
if(isTRUE(condition)==TRUE) {do something}

Related

While trying to run through effiicient age calculation I got this error in R [duplicate]

I received this error message:
Error in if (condition) { : missing value where TRUE/FALSE needed
or
Error in while (condition) { : missing value where TRUE/FALSE needed
What does it mean, and how do I prevent it?
The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.
if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed
This can happen accidentally as the results of calculations:
if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed
To test whether an object is missing use is.na(x) rather than x == NA.
See also the related errors:
Error in if/while (condition) { : argument is of length zero
Error in if/while (condition) : argument is not interpretable as logical
if (NULL) {}
## Error in if (NULL) { : argument is of length zero
if ("not logical") {}
## Error: argument is not interpretable as logical
if (c(TRUE, FALSE)) {}
## Warning message:
## the condition has length > 1 and only the first element will be used
I ran into this when checking on a null or empty string
if (x == NULL || x == '') {
changed it to
if (is.null(x) || x == '') {
this works with "NA" not for NA
comments = c("no","yes","NA")
for (l in 1:length(comments)) {
#if (!is.na(comments[l])) print(comments[l])
if (comments[l] != "NA") print(comments[l])
}
I was getting this same error in my forloops with complex if statements. I fixed this issue by just wrapping my condition with isTRUE.
if(isTRUE(condition)==TRUE) {do something}

understanding ifelse with NAs [duplicate]

I received this error message:
Error in if (condition) { : missing value where TRUE/FALSE needed
or
Error in while (condition) { : missing value where TRUE/FALSE needed
What does it mean, and how do I prevent it?
The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.
if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed
This can happen accidentally as the results of calculations:
if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed
To test whether an object is missing use is.na(x) rather than x == NA.
See also the related errors:
Error in if/while (condition) { : argument is of length zero
Error in if/while (condition) : argument is not interpretable as logical
if (NULL) {}
## Error in if (NULL) { : argument is of length zero
if ("not logical") {}
## Error: argument is not interpretable as logical
if (c(TRUE, FALSE)) {}
## Warning message:
## the condition has length > 1 and only the first element will be used
I ran into this when checking on a null or empty string
if (x == NULL || x == '') {
changed it to
if (is.null(x) || x == '') {
this works with "NA" not for NA
comments = c("no","yes","NA")
for (l in 1:length(comments)) {
#if (!is.na(comments[l])) print(comments[l])
if (comments[l] != "NA") print(comments[l])
}
I was getting this same error in my forloops with complex if statements. I fixed this issue by just wrapping my condition with isTRUE.
if(isTRUE(condition)==TRUE) {do something}

simple for loop doesnt work because of the condition error [duplicate]

I received this error message:
Error in if (condition) { : missing value where TRUE/FALSE needed
or
Error in while (condition) { : missing value where TRUE/FALSE needed
What does it mean, and how do I prevent it?
The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.
if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed
This can happen accidentally as the results of calculations:
if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed
To test whether an object is missing use is.na(x) rather than x == NA.
See also the related errors:
Error in if/while (condition) { : argument is of length zero
Error in if/while (condition) : argument is not interpretable as logical
if (NULL) {}
## Error in if (NULL) { : argument is of length zero
if ("not logical") {}
## Error: argument is not interpretable as logical
if (c(TRUE, FALSE)) {}
## Warning message:
## the condition has length > 1 and only the first element will be used
I ran into this when checking on a null or empty string
if (x == NULL || x == '') {
changed it to
if (is.null(x) || x == '') {
this works with "NA" not for NA
comments = c("no","yes","NA")
for (l in 1:length(comments)) {
#if (!is.na(comments[l])) print(comments[l])
if (comments[l] != "NA") print(comments[l])
}
I was getting this same error in my forloops with complex if statements. I fixed this issue by just wrapping my condition with isTRUE.
if(isTRUE(condition)==TRUE) {do something}

Comparing two strings in if else [duplicate]

I received this error message:
Error in if (condition) { : missing value where TRUE/FALSE needed
or
Error in while (condition) { : missing value where TRUE/FALSE needed
What does it mean, and how do I prevent it?
The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.
if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed
This can happen accidentally as the results of calculations:
if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed
To test whether an object is missing use is.na(x) rather than x == NA.
See also the related errors:
Error in if/while (condition) { : argument is of length zero
Error in if/while (condition) : argument is not interpretable as logical
if (NULL) {}
## Error in if (NULL) { : argument is of length zero
if ("not logical") {}
## Error: argument is not interpretable as logical
if (c(TRUE, FALSE)) {}
## Warning message:
## the condition has length > 1 and only the first element will be used
I ran into this when checking on a null or empty string
if (x == NULL || x == '') {
changed it to
if (is.null(x) || x == '') {
this works with "NA" not for NA
comments = c("no","yes","NA")
for (l in 1:length(comments)) {
#if (!is.na(comments[l])) print(comments[l])
if (comments[l] != "NA") print(comments[l])
}
I was getting this same error in my forloops with complex if statements. I fixed this issue by just wrapping my condition with isTRUE.
if(isTRUE(condition)==TRUE) {do something}

Error in R equivalence comparison: missing value where TRUE/FALSE needed [duplicate]

I received this error message:
Error in if (condition) { : missing value where TRUE/FALSE needed
or
Error in while (condition) { : missing value where TRUE/FALSE needed
What does it mean, and how do I prevent it?
The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.
if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed
This can happen accidentally as the results of calculations:
if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed
To test whether an object is missing use is.na(x) rather than x == NA.
See also the related errors:
Error in if/while (condition) { : argument is of length zero
Error in if/while (condition) : argument is not interpretable as logical
if (NULL) {}
## Error in if (NULL) { : argument is of length zero
if ("not logical") {}
## Error: argument is not interpretable as logical
if (c(TRUE, FALSE)) {}
## Warning message:
## the condition has length > 1 and only the first element will be used
I ran into this when checking on a null or empty string
if (x == NULL || x == '') {
changed it to
if (is.null(x) || x == '') {
this works with "NA" not for NA
comments = c("no","yes","NA")
for (l in 1:length(comments)) {
#if (!is.na(comments[l])) print(comments[l])
if (comments[l] != "NA") print(comments[l])
}
I was getting this same error in my forloops with complex if statements. I fixed this issue by just wrapping my condition with isTRUE.
if(isTRUE(condition)==TRUE) {do something}

Resources