jinnsuke
jinnsuke

Reputation: 11

How do I solve the error - missing value where TRUE/FALSE needed

delaylevel <- c()
delaysort <- function(delaycol){
  for (x in delaycol){
    if (x > 60){
      append(delaylevel, 3)
    }else if (x > 30){
      append(delaylevel, 2)
    }else if (x > 15){
      append(delaylevel, 1)
    }else{
      append(delaylevel, 0)
    }
  }
}

sapply(flights$ARRIVAL_DELAY, delaysort)

flights$ARRIVAL_DELAY is a column from a data frame (vector). I am able to apply each element individually to get a TRUE or FALSE, but it does not work with this function. What should be changed to make the fuction work?

Upvotes: 0

Views: 404

Answers (1)

s_baldur
s_baldur

Reputation: 33488

Efficiency aside... you need to assign after you use append() and then also return the result from the function:

delaysort <- function(delaycol){
  delaylevel <- c()
  for (x in delaycol){
    if (x > 60){
      delaylevel = append(delaylevel, 3)
    }else if (x > 30){
      delaylevel = append(delaylevel, 2)
    }else if (x > 15){
      delaylevel = append(delaylevel, 1)
    }else{
      delaylevel = append(delaylevel, 0)
    }
  }
  return(delaylevel)
}

sapply(1:70, delaysort)
# [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3

A faster and more R'ish would be using cut or similar:

delaysort <- function(delaycol) as.integer(cut(1:70, c(0, 15, 30, 60, Inf))) - 1
delaysort(delaycol)
# [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3

Upvotes: 2

Related Questions