How would I make this comparison in R?
if (is.null(a)| a == 'LAST') {
# do something
}
If the a is NULL this fails, throwing the error:
Error in if (a == "LAST") { :
argument is of length zero
How could I work around this?
Related
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}
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}
Hello I have the following code;
valorNegativos = function (vela) {
for(a in 1:length(vela)) {
apply(vela[[a]], 2, function(col) ifelse( length(as.numeric(col) < 0) == length(vela[[a]]$FECHA), print(paste0("Correct data")), stop("ERROR: Negative values"))
}
}
vela, is a list of dataframes.
What I have to do with this function is to prove if all the columns values from all my dataframes are positive ( > 0 ) and to check if the number of values is equal to the value count from one column $FECHA ( date ).
The problem is that I am getting the two posibilites from the ifelse.
[1] "Correct data"
Show Traceback
Rerun with Debug
Error in ifelse(length(as.numeric(col) > 0) == length(vela[[a]]$FECHA, :
ERROR: Negative values
Any idea?
I'm trying to check a range of numbers against some values in a dataset using for control
for(i in 20:28)
{
for(j in 1:52)
{
if (Test$Ferritin[j]<15 & Test$RHCc[j]<i)
{
Test$Status[j] = "TP"
}
}
}
But I keep getting the error
Error in if (Test$Ferritin[j] < 15 & Test$RHCc[j] < i) { : argument
is of length zero
I did check the condition using is.null, but it returns "False" in the answer.
Can someone explain what I could be doing wrong?
NULL is of length 0, but not all length zero vectors are NULL (you can confirm for yourself via is.null(numeric(0))). Check whether length(argument) == 0 instead.
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}