Error in If/While (Condition) {: Missing Value Where True/False Needed

Error in if/while (condition) {: missing Value where TRUE/FALSE needed

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

Error in if (condition) {: missing Value where TRUE/FALSE needed

  1. "NA" is not the same as NA (which might be appropriate there.

  2. for (i in esg.ordered) is wrong: it is iterating over each column in your frame named esg.ordered, so i is a full vector. I think you mean for (i in seq_len(nrow(esg.ordered))).

  3. The error missing value where TRUE/FALSE needed is easily searched and should return (among other links) Error in if/while (condition) {: missing Value where TRUE/FALSE needed. It is because the conditional in if is returning NA.

  4. You appear to be doing something on a whole vector at a time, this is a literal translation of what you are trying to do (but without the for loop):

    esg.ordered$flowpct <- ifelse(
    c(TRUE, esg.ordered$fundid[-1] == esg.ordered$fundid[-nrow(esg.ordered)]),
    esg.ordered$flow / c(NA, esg.ordered$size[-nrow(esg.ordered)]),
    NA)
    esg.ordered
    # fundid size flow flowpct
    # 1 FS00008KNP 78236537 7038075.4 NA
    # 2 FS00008KNP 73048868 -5691940.6 -0.072752972
    # 3 FS00008KNP 74688822 -193188.8 -0.002644651
    # 4 FS00008KNP 95330799 11991514.1 0.160552996
    # 5 FS00008L0W 44170465 -15706588.7 NA
    # 6 FS00008L0W 33278560 -12749545.9 -0.288644140
    # 7 FS00008L0W 26084262 -6879079.2 -0.206712045
    # 8 FS00008L0W 23857701 -3227825.0 -0.123746075

    However, the relies wholly on fundid being ordered correctly. I think a safer way to go is this, using ave to get the last size within the current fundid, and then dividing:

    esg.ordered$flowpct <- with(esg.ordered,
    flow / ave(size, fundid, FUN = function(z) c(NA, z[-length(z)])))

    Same results as above, but much safer.

Strange error: Error in if () { : missing value where TRUE/FALSE needed

It may be easier to use subset

maschi <- subset(studenti, sesso == "M")

or filter from dplyr

library(dplyr)
studenti %>%
filter(sesso == "M")

The for loop can be done with rbind

maschi <- studenti[0,]
for(i in seq_len(nrow(studenti))) {
if(studenti$sesso[i] == "M" & !is.na(studenti$sesso[i])) {
maschi <- rbind(maschi, studenti[i,])
}
}

Also, in the OP's code, we just need

maschi <- studenti[0,]
indice = 0

for (i in seq_along(studenti$sesso)) {
if (studenti$sesso[i] == "M" & !is.na(studenti$sesso[i])) {
indice = indice + 1
maschi[indice,] <- studenti[i,]
}
}

-output

> maschi
sesso altezza
2 M 182
3 M 176
4 M 193

-Update using the OP's update data

> maschi
sesso altezza peso scarpe caso matricola mese giorno
2 M 182 75 43 9 -1 5 2
3 M 176 72 45 5 -1 9 29
4 M 193 95 45 6 -1 5 8
5 M 178 68 45 7 6 7 12

data

studenti <- data.frame(sesso = c("F", "M", "M", "M"), altezza = c(168, 182, 176, 193))

Error: missing value where TRUE/FALSE needed in while loop

In the second iteration sum[k+1] = NA since it'll be evaluated to:

(sum[2+1]-sum[1])<10 where sum[2+1] = sum[3] is NA. So (sum[k+1]-sum[k])<10 will not be evaluated to one of TRUE/FALSE.

Iteration (k) | sum[k+1]-sum[k]
--------------+------------------
1 | sum[2] - sum[1] They're both known
2 | sum[3] - sum[2] What is sum[3]? (NA)


Related Topics



Leave a reply



Submit